In TestClass.pm replace all instances of $self{$key}
with $self->{$key}
(lines 16 and 24), and %self
with %$self
(line 23).
Your output will look like this:
This is correct for test1.txt
myfile => test1.txt
VAL1 => 1
This is wrong for test1.txt, test2.txt overwrote test1.txt
myfile => test1.txt
VAL1 => 1
myfile => test2.txt
VAL1 => 2
On a side note, if you use strict;
Perl will catch these sorts of errors for you.