#!usr/bin/perl use strict; use warnings; use Data::Dumper; # use Benchmark qw(:all) ; # WindowsOS use Benchmark::Forking qw( timethese cmpthese ); # LinuxOS my @preserved = @ARGV; =Hash Of Arrays my %HoA; while (<>) { chomp; push @{ $HoA{$ARGV} }, $_; } continue { close ARGV if eof; } print Dumper \%HoA; __END__ $ perl test.pl in.txt out.txt $VAR1 = { 'in.txt' => [ 'Sample of text line 1', '', 'Sample of text line 2' ], 'out.txt' => [ 'Sample of text line 1 second file', '', 'Sample of text line 2 second file' ] }; =cut sub while_loop { my @output; @ARGV = @preserved; # restore original @ARGV while (<>) { chomp; push @output, $_; } continue { close ARGV if eof; } return; } sub file_to_array { @ARGV = @preserved; # restore original @ARGV chomp (my @data = <>); close ARGV if eof; return; } my $results = timethese(10000000, { Array => \&file_to_array, While => \&while_loop }, 'none'); cmpthese( $results ); __END__ $ perl test.pl in.txt in2.txt Rate Array While Array 115513/s -- -4% While 120846/s 5% --