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

Posted on 2006-03-30
Last Modified: 2012-08-14
#!/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;

Question by:davidgareau
    LVL 39

    Accepted Solution

    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

    Author Comment

    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
    LVL 39

    Assisted Solution

    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:

    Author Comment

    cool, thanks Adam.... great answers
    LVL 39

    Expert Comment

    thanks for the points  :)

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
    A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now