perl - Reading file line by line iteration issue -
i have next simple piece of code (identified problem piece of code , extracted much larger program).
is me or can see obvious error in code stopping matching against $variable
, printing $found
when should doing?
nothing printed when seek print $variable
, , there matching lines in file using.
the code:
if (defined $var) { open (messages, "<$messages") or die $!; $thetext = $mech->content( format => 'text' ); print "$thetext\n"; foreach $variable (<messages>) { chomp ($variable); print "$variable\n"; if ($thetext =~ m/$variable/) { print "found\n"; } } }
i have located point @ error occurring cannot understand why? there may totally overlooking late?
update have since realised misread question , doesn't solve problem. points valid leaving them here.
you have regular look metacharacters in $variable
. line
if ($thetext =~ m/$variable/) { ... }
should be
if ($thetext =~ m/\q$variable/) { ... }
to escape there are.
but sure don't want eq
?
in addition, should read file using
while (my $variable = <messages>) { ... }
as for
loop unnecessarily read entire file memory. , please utilize improve name $variable
.
perl file for-loop
No comments:
Post a Comment