use strict; use warnings; use Tie::IxHash; use Data::Dumper; my ($alarm_details) = &main(); print Dumper @$alarm_details; sub main{ my @alarm_details; my @paths = qw(c:/test/sample.xml c:/test/sample1.xml); #giving the file path foreach my $path (@paths){ my %alarm_details; #Declare Each time instead of setting null value tie %alarm_details, "Tie::IxHash"; #for Hash in order open(LOG,"<$path") || die "can't open log "; $alarm_details{$path} = $path; while () { chomp; /^(.*)=(.*)$/; $alarm_details{ $1 } = $2 if ($1); } push(@alarm_details,\%alarm_details); } return \@alarm_details; }