2010-11-22 9 views
2

おはようございます。私はプログラミングが初めてです。そして、私はPerlについて深く掘り下げたいと思います。 私はMechanizeの例を持っています - 単純ですが、私にとっては複雑すぎる:説明が必要です。これであなたの助けが必要です!機械化の例 - 私にとって単純すぎるが複雑すぎる:説明が必要

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"; 
    } 

誰でも私にラインごとの説明を教えていただけますか?

+7

あなたが言うようにプログラミングを初めてお使いの方は、オブジェクト、ネットワーキング、およびネストされたデータ構造を使用しないコードから始めたいと思うかもしれません。これらは、言語の基本的な構文をもっと難しくすることを試みるすべてのトピックです。 Perlの学習を始める方法については、いくつかの検索を試してみてください。 –

+6

@エリック私はこれがhttp://stackoverflow.com/users/515484/apollomanおよびhttp://stackoverflow.com/users/515336/super-fast-birdとhttp://stackoverflow.comと同じ人だと信じています。/users/477580/thebutcherなどがあります。 –

+0

@Sinan =>可能性があります...何をしているのですか? –

答えて

3

私は最初のクーペのラインを試しました。 - 特に変数のスコープ部分

2)Perl data

3)Perl Data Structures Cookbook

1)Perl Intro:あなたは最初よく読んでくださいと、次のマニュアルを理解する必要があるしかし

PSコメントでEricが言ったように、このコードは間違いなく始まったばかりの人のための非常に良い例ではありません。あまりにも多くの些細なアイデアやコンセプト、可動部品があります。

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"; 
} 
+0

こんにちはDVK - 多くのおかげで:スクリプトは、ページDespairからいくつかのリンクを取得しますか?それはそれらをフェッチするのですか? – zero

+0

こんにちはDVK - あなたの男R O C K !!!それははるかに予想以上です。多くの皆さん、ありがとうございました!これは多くを学ぶ絶好の機会です! THX - あなたは素晴らしいです! – zero

+1

@billie - いくつかの点:#1 - 上記のEricのコメントをご覧ください。あなたは非常に初心者ではないコードを選んだ。 – DVK

関連する問題