0

This little snippet is from the first chapter of the LWP perl oreilly book. This line

$count++ while $catalog =~ m/Perl/gi; 

perplexes me

I do not understand how the while statement iterates through the lines in the $catalog variable to find the matched, I don't even know how to explain what that line does in english much less perl

#!/usr/bin/perl -w
use strict ;
use LWP::Simple ;
my $catalog = get("http://www.oreilly.com/catalog");
my $count = 0;
$count++ while $catalog =~ m/Perl/gi;
print "$count\n";

so I have tried writing it out long hand to no avail.

#!/usr/bin/perl -w
use strict ;
use LWP::Simple ;
my $catalog = get("http://www.oreilly.com/catalog");


open( my $fh_catalog ,"<" , $catalog) || die "cant open $!";
while (<$fh_catalog>) {
    print $_ ;
    sleep 1;
}

I even tried

#!/usr/bin/perl -w
use strict ;
use LWP::Simple ;
my $catalog = get("http://www.oreilly.com/catalog");

while (<$catalog>) {
    print $_ ;
    sleep 1;
}
0

1 Answer 1

4

$catalog contains the string <!DOCTYPE HTML PUB[...][newline][newline]<html>[...].

Your first snippet fails because $catalog doesn't contain a file name.

Your second snippet fails because $catalog doesn't contain a file handle.

When a match operator with the /g modifier is used scalar context, it searches from where the last search left off.

The analog would be

use Time::HiRes qw( sleep );  # Support sleeping fractions of seconds.
$| = 1;  # Turn off STDOUT's output buffering.
for my $i (0..length($content)-1) {
   print(substr($content, $i, 1));
   sleep 0.1;
}

Let's use a simpler string as an example.

my $s = "a000a000a000";
++$count while $s =~ /a/g;

Here's what happens:

  1. The match operator is executed. It finds the first a, sets pos($s) = 1;, and returns true.
  2. The loop body is entered, and $count is incremented.
  3. The match operator is executed. It behaves as if the string started as pos($s) (1), finds the second a, sets pos($s) = 5;, and returns true.
  4. The loop body is entered, and $count is incremented.
  5. The match operator is executed. It behaves as if the string started as pos($s) (5), finds the third a, sets pos($s) = 9;, and returns true.
  6. The loop body is entered, and $count is incremented.
  7. The match operator is executed. It behaves as if the string started as pos($s) (9), fails to find a match, clears pos($s), and returns false. The loops exits.

Nothing changes if some of the characters of the string are newlines.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you Ikegami - I knew global meant search the whole document, but did not understand how it searched globally. watching the analog version print out is the coolest.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.