use strict, my & =~{Perl}gi ------ David's Perl Project (Day 4 of 60)

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

Hello, I have been reading a book "Perl and LWP" and came across this script.  I haven't come to a few things in my other readings yet, but would like to know what they are:

What is the 'my' keyword for, what does it change for example in the line
my $count = 0;
as opposed to simply writing
$count = 0;

use strict; # ????????????????
use LWP::Simple;   #says that I'm going to be using script from the LWP::Simple module that I installed

$count++ while $catalog =~ m{Perl}gi;
This increments the scalar $count by one each timet eh string 'Perl' is found in the scalar $catalog, which is basically just the HTML source code from the site scraped with the get() function, correct?  What does the last part, or rather how do I know how to stucture something similar to this, the ~, m, {} and gi threw me off:
=~ m{Perl}gi;

Who is Participating?
1. What is the 'my' keyword for...
my declares a variable to be local to the enclosing block

2.use strict;
requires variables to be declared

use LWP::Simple;
Uses the module LWP::Simple.  When including a module, the :: is converted to the path seperator, and it looks for a file of the given name in the library directories.  So this module is in the LWP directory, and has a name

3. When doing a regular expression pattern match, the syntax is
$seach =~ m/pattern/<options>
So the =~ means to run a regular expression
the m means to pattern match
the {} can be any character.  The typical characters are /, but it can be anything.
The g at the end means global... keeps searching as long as there is a match
the i at the end means ignore case

See this for more info
davidgareauAuthor Commented:
1 Thanks

2. I don't see how variables here are declared, how would it have to be written if they weren't declared I thought you didn't have to declare in Perl, what's the benefit of declaring?

3. Thanks
By default, you don't have to declare variables.  The use strict changes that.

There are 3 kinds of strict-ness: vars, subs, and refs.  
use strict;  #turns on all three strict
use strict vars;   #turns on strict for vars only
use strict subs;   #turns on strict for subs only
use strict refs;    #turns on strict for refs only

What's the benefit?  It help eliminate problems.

my $VarWithLongName="hello";
print "value=$VarWithName\n";  

with stict turned on, the second line will be an error, because $VarWithName doesn't exist (it hasn't been defined).  Whereas without strict, it would allow it, and $VarWithName would just be undefined.

For more info:
davidgareauAuthor Commented:
cool, thanks Adam.... great answers
thanks for the points  :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.