in reply to Re: how to open page and follow link
in thread how to open page and follow link

Hi. Thanks for your help.

I am running this code

#!/usr/bin/perl use warnings; use strict; my $url = "(removed)"; use CGI::Carp qw(fatalsToBrowser); use CGI qw/:standard/; use lib "/home/name/public_html/lib/"; use WWW::Mechanize; my $mech = WWW::Mechanize->new(); $mech->get( $url ); print header, start_html(); print $mech->links;
And is printing out
WWW::Mechanize::Link=ARRAY(0xa430d5c)WWW::Mechanize::Link=ARRAY(0xa442 +a70)WWW::Mechanize::Link=ARRAY(0xa448fe4)WWW::Mechanize::Link=ARRAY(0 +xa448f00)WWW::Mechanize::Link=ARRAY(0xa444f24)WWW::Mechanize::Link=AR +RAY(0xa4445e0)WWW::Mechanize::Link=ARRAY(0xa442c5c)WWW::Mechanize::Li +nk=ARRAY(0xa4446c4)WWW::Mechanize::Link=ARRAY(0xa444f00)WWW::Mechaniz +e::Link=ARRAY(0xa44c7a0)WWW::Mechanize::Link=ARRAY(0xa448fa8)WWW::Mec +hanize::Link=ARRAY(0xa4450c8)WWW::Mechanize::Link=ARRAY(0xa444670)WWW +::Mechanize::Link=ARRAY(0xa449f44)WWW::Mechanize::Link=ARRAY(0xa44478 +4)WWW::Mechanize::Link=ARRAY(0xa44d31c)WWW::Mechanize::Link=ARRAY(0xa +449efc)WWW::Mechanize::Link=ARRAY(0xa44a004)WWW::Mechanize::Link=ARRA +Y(0xa44e664)WWW::Mechanize::Link=ARRAY(0xa4490f8)WWW::Mechanize::Link +=ARRAY(0xa449f80)WWW::Mechanize::Link=ARRAY(0xa44d664)WWW::Mechanize: +:Link=ARRAY(0xa449c5c)WWW::Mechanize::Link=ARRAY(0xa449ef0)WWW::Mecha +nize::Link=ARRAY(0xa44e6e8)WWW::Mechanize::Link=ARRAY(0xa451b98)WWW:: +Mechanize::Link=ARRAY(0xa44d574)WWW::Mechanize::Link=ARRAY(0xa449d1c) +WWW::Mechanize::Link=ARRAY(0xa4491a0)WWW::Mechanize::Link=ARRAY(0xa45 +2c70)
Did I do something wrong?

Thanks.

Replies are listed 'Best First'.
Re^3: how to open page and follow link
by ikegami (Patriarch) on Apr 11, 2005 at 03:15 UTC

    links returns an array of objects of type WWW::Mechanize::Link, not an array of strings. Perhaps you want:

    print $_->url(), "\n" foreach $mech->links();
      Yes, that did work. Thanks.

      I tried my own version of (see below) and to my suprise, it didn't work! lol.

      foreach ($mech->links()) { print "$_<br>"}
      But that's okay since yours does. Only reason I wanted to do this was to see what # my link was but from the print out, I don't think the printout order is the order I need in order to follow a specific link.

      Before I try

      $mech->get( $url ); $mech->follow_link( url_regex => qr/Frogs and Toads/i );
      I need to know I'm doing this on the right URL. Is there a way I can check this link? I tried using find_link and printing it out but it's printing out "index.cgi" and NOT the Frogs and Toads link so I have a feeling if I use follow_link it won't be going to the right link.

      How can I be sure?

      Thanks and sorry for the noobie questions

        I tried my own version of (see below) and to my suprise, it didn't work! lol.

        links returns an array of objects of type WWW::Mechanize::Link, not an array of strings.
        foreach ($mech->links()) { print "$_<br>" }
        should be
        foreach ($mech->links()) { print $_->url(), "<br>" }

        Is there a way I can check this link?

        Is there any reason to believe it's not? Why are you assuming the module isn't working? WWW::Mechanize::Link has a method called text if you can get the link as an object of that type.

Re^3: how to open page and follow link
by bart (Canon) on Apr 11, 2005 at 11:20 UTC
    Perhaps it'd be nice if WWW::Mechanize::Link had '""' overloaded to return the URL, when stringified. It would produce more "intuitive" results for normal use.

    You can currently produce that result by adding this to your code:

    use WWW::Mechanize; BEGIN { package WWW::Mechanize::Link; use overload '""' => \&url_abs; }

    OTOH it wouldn't do much good in your case, as you're trying to generate a HTML page of links. A simple, plain text, list of links then isn't much good.