my %filedata; for my $i ( 2 .. 3 ) { my $fname = sprintf( "T%dT.txt", $i ); open( I, $fname ) or die "$fname: $!\n"; local $/; # temporarily sets input record separator to "undef" $filedata{$fname} = ; close I; } # you now have the full content of each file as a single string value, # indexed by file name in %filedata