use strict; use warnings; use 5.010; my $fname = 'somefile.php'; #Slurp whole file: my $file; { local $/ = undef; $file = ; } my %files_for; while($file =~ m{ <[?]php \s* (.*?) \s* [?]> }xmsg) { my $php_code = $1; push @{ $files_for{$php_code} }, $fname; } use Data::Dumper; say Dumper(\%files_for); __END__
hello
--output:-- $VAR1 = { 'echo \'world\';' => [ 'somefile.php', 'somefile.php' ], 'echo \'hello\';' => [ 'somefile.php' ] };