my @lines = <$fh>; # read whole file
foreach (0 .. $#lines)
{
if ($lines[$_] =~ /$regex/)
{
printf "Match found on line %d\n", ($_ + 1);
$match = 1;
}
}
No need to read the whole file at once:
while ( <$fh> )
{
if ( /$regex/ )
{
print "Match found on line $.\n";
$match = 1;
}
}
This may solve your multi-line problem (UNTESTED):
my $lines;
while ( <$fh> )
{
$lines .= $_;
if ( $lines =~ /($regex)/sm )
{
my $newlines = $1 =~ tr/\n//;
if ( $newlines )
{
print "Match found on lines ", $. - $newlines, " through $
+.\n";
}
else
{
print "Match found on line $.\n";
}
$match = 1;
$lines = $_;
}
}