We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

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

davidgareau
davidgareau asked
on
Medium Priority
472 Views
Last Modified: 2012-08-14
#!/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;

Comment
Watch Question

Top Expert 2009
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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
Top Expert 2009
Commented:
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

Author

Commented:
cool, thanks Adam.... great answers
Top Expert 2009

Commented:
thanks for the points  :)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.