• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 447
  • Last Modified:

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;

0
davidgareau
Asked:
davidgareau
  • 3
  • 2
2 Solutions
 
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
0
 
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
0
 
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
0
 
davidgareauAuthor Commented:
cool, thanks Adam.... great answers
0
 
Adam314Commented:
thanks for the points  :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now