Search code examples
perlmechanizewww-mechanize

How to use output from WWW::Mechanize?


I would like to loop through all links on a web page, so I have tried

#!/usr/bin/perl

use WWW::Mechanize;

my $url = "http://www.google.com";
my $m = WWW::Mechanize->new();
$m->get($url);

my @links = $m->find_all_links(url_regex => qr/google/);

foreach my $link (@links){
    print Dumper $m->get($link->url_abs);
}     

which gives me e.g.

$VAR11 = bless( [
                  'http://www.google.com/ncr',
                  'Google.com in English',
                  undef,
                  'a',
                  $VAR1->[4],
                  {
                    'href' => 'http://www.google.com/ncr',
                    'class' => 'gl nobr'
                  }
                ], 'WWW::Mechanize::Link' );

Question

How do I output just the links?


Solution

  • The documentation points out that the links are returned as WWW::Mechanize::Link objects. Therefore:

    my @links = $m->find_all_links(url_regex => qr/google/);
    print $_->url, "\n" for @links;