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("http://www.oreilly.com/catalog");
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:

1.
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;

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

3.
$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;

davidgareauAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Adam314Commented:
1. What is the 'my' keyword for...
my declares a variable to be local to the enclosing block
http://perldoc.perl.org/functions/my.html

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 Simple.pm.

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
http://perldoc.perl.org/perlop.html#Regexp-Quote-Like-Operators-operator%2c-regexp

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Adam314Commented:
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.

eg:
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:
http://www.perl.com/pub/a/2001/01/begperl6.html
davidgareauAuthor Commented:
cool, thanks Adam.... great answers
Adam314Commented:
thanks for the points  :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.