2

Good day dear community. I am new to programming. And i want to digg deeper into Perl. So i have a Mechanize example - quiete simple but too complex for me: need explanations. I need your help here with this!

use strict;
    $|++;

    use WWW::Mechanize;
    use File::Basename;

    my $m = WWW::Mechanize->new;

    $m->get("http://www.despair.com/indem.html");

    my @top_links = @{$m->links};

    for my $top_link_num (0..$#top_links) {
        next unless $top_links[$top_link_num][0] =~ /^http:/;

        $m->follow_link( n=>$top_link_num ) or die "can't follow $top_link_num";

        print $m->uri, "\n";
        for my $image (grep m{^http://store4}, map $_->[0], @{$m->links}) {
            my $local = basename $image;
            print " $image...", $m->mirror($image, $local)->message, "\n"
        }

        $m->back or die "can't go back";
    }

can anybody give me a line by line explanation?

DVK
  • 126,886
  • 32
  • 213
  • 327
zero
  • 1,003
  • 3
  • 20
  • 42
  • 7
    If you are new to programming as you say, you might want to start off with some code that doesn't use objects, networking, and nested data structures. These are all topics that will make trying to learn the basic syntax of the language more difficult. There are many SO questions about how to start learning Perl, try a few searches. – Eric Strom Nov 22 '10 at 18:34
  • 6
    @Eric I believe this is the same person as http://stackoverflow.com/users/515484/apolloman and http://stackoverflow.com/users/515336/super-fast-bird and http://stackoverflow.com/users/477580/thebutcher and possibly others. – Sinan Ünür Nov 22 '10 at 19:10
  • @Sinan => Could be... What's the point of doing that? – Eric Strom Nov 22 '10 at 19:39
  • 1
    @Eric - do not meddle in the motivations of trolls, for they are prodigious and quick to post :) – DVK Nov 22 '10 at 20:58
  • 1
    @DVK: Do not meddle in the affairs of SO, for it is subtle and quick to anger :) – Piskvor left the building Nov 22 '10 at 21:37

1 Answers1

3

I tried the first coupe of lines.

However you need to make sure to first read and understand the following documentation:

1) Perl Intro - especially variable scoping part

2) Perl data

3) Perl Data Structures Cookbook

P.S. As Eric said in the comment, this code is definitely NOT a very good example for someone just starting. It's got way too many non-trivial ideas/concepts/moving parts.

use strict; 
  # Does not allow undeclared global variables or other unsafe constructs.
  # You should ALWAYS code with "use strict; use warnings"
  # See http://perldoc.perl.org/strict.html
$|++;
  # Turn on autoflush on STDOUT filehandle. 
  # See "http://perldoc.perl.org/perlvar.html" for "$|" and other special variables.
  # P.S. This "++" is a hack - it would be a lot more readable to do "$| = 1;"
  #      since $| only cares whether the value is zero or non-zero.

use WWW::Mechanize; # Load the module for getting web sites.
use File::Basename; # Load the module for finding script's name/path.

my $m = WWW::Mechanize->new; # Create new object via a constructor (new)

$m->get("http://www.despair.com/indem.html"); 
   # Retrieve the contents of the URL. 
   # See http://search.cpan.org/dist/WWW-Mechanize/lib/WWW/Mechanize.pm
   # for the module's documentation (aka POD)

my @top_links = @{$m->links};
   # Declare a "@top_links" array, 
   # get the list of links on the above page (returns array reference)
   # and de-reference that array reference and store it in @top_links array

for my $top_link_num (0..$#top_links) { 
    # Loop over all integers between 0 and the last index of @top_links array
    # (e.g. if there were 3 links, loop over 0,1,2
    # Assign the current loop value to $top_link_num variable

    next unless $top_links[$top_link_num][0] =~ /^http:/;
    # go to next iteration of the loop unless the current link's URL is HTTP protocol
    # Current link is the element of the array with current undex -
    #    $top_links[$top_link_num]
    # The link data is stored as an array reference,
    # with the link URL being the first element of the arrayref 
    # Therefore, $top_links[$top_link_num][0] - which is the shorthand 
    #    for $top_links[$top_link_num]->[0] as you learned
    #    from reading Data Structures Cookbook I linked - is the URL
    # To check if URL is HTTP prtocol, we check if it starts with http:
    # via regular expression - see "http://perldoc.perl.org/perlre.html"

    $m->follow_link( n=>$top_link_num ) or die "can't follow $top_link_num";

    print $m->uri, "\n";
    for my $image (grep m{^http://store4}, map $_->[0], @{$m->links}) {
        my $local = basename $image;
        print " $image...", $m->mirror($image, $local)->message, "\n"
    }

    $m->back or die "can't go back";
}
DVK
  • 126,886
  • 32
  • 213
  • 327
  • Hello DVK - Many thanks: Does the script fetch some links from the page Despair? Does it fetch them? – zero Nov 22 '10 at 18:31
  • hi DVK - you guy R O C K !!! THAT is far more than expected. Many many thanks for all you did! This is a great chance to learn alot!! THX - you are great! – zero Nov 22 '10 at 18:41
  • 1
    @billie - A couple of points: #1 - please see Eric's comment above. You picked code which is NOT very beginner friendly. – DVK Nov 22 '10 at 18:44
  • @billie - #2. you're welcome. Please feel free to show your appreciation of the answer(s) that help you by eith up-voting them (up arrow next to the answer) and/or accepting the most helpful one (checkmark next to the answer). – DVK Nov 22 '10 at 18:45
  • 1
    @DVK you are wasting your time. – Sinan Ünür Nov 22 '10 at 19:11
  • 1
    @Sinan - yeah, figured that from your research, as well as lack of upvote from the guy. – DVK Nov 22 '10 at 20:59