perl while loop iterates through a variable. -
perl while loop iterates through a variable. -
this little snippet first chapter of lwp perl oreilly book. line
$count++ while $catalog =~ m/perl/gi; perplexes me
i not understand how while statement iterates through lines in $catalog variable find matched, don't know how explain line in english language much less perl
#!/usr/bin/perl -w utilize strict ; utilize lwp::simple ; $catalog = get("http://www.oreilly.com/catalog"); $count = 0; $count++ while $catalog =~ m/perl/gi; print "$count\n"; so have tried writing out long hand no avail.
#!/usr/bin/perl -w utilize strict ; utilize lwp::simple ; $catalog = get("http://www.oreilly.com/catalog"); open( $fh_catalog ,"<" , $catalog) || die "cant open $!"; while (<$fh_catalog>) { print $_ ; sleep 1; } i tried
#!/usr/bin/perl -w utilize strict ; utilize lwp::simple ; $catalog = get("http://www.oreilly.com/catalog"); while (<$catalog>) { print $_ ; sleep 1; }
$catalog contains string <!doctype html pub[...][newline][newline]<html>[...].
your first snippet fails because $catalog doesn't contain file name.
your sec snippet fails because $catalog doesn't contain file handle.
when match operator /g modifier used scalar context, searches lastly search left off.
the analog be
use time::hires qw( sleep ); # back upwards sleeping fractions of seconds. $| = 1; # turn off stdout's output buffering. $i (0..length($content)-1) { print(substr($content, $i, 1)); sleep 0.1; } let's utilize simpler string example.
my $s = "a000a000a000"; ++$count while $s =~ /a/g; here's happens:
the match operator executed. finds firsta, sets pos($s) = 1;, , returns true. the loop body entered, , $count incremented. the match operator executed. behaves if string started pos($s) (1), finds sec a, sets pos($s) = 5;, , returns true. the loop body entered, , $count incremented. the match operator executed. behaves if string started pos($s) (5), finds 3rd a, sets pos($s) = 9;, , returns true. the loop body entered, , $count incremented. the match operator executed. behaves if string started pos($s) (9), fails find match, clears pos($s), , returns false. loops exits. nothing changes if of characters of string newlines.
perl
Comments
Post a Comment