SpacemanSpiff has asked for the wisdom of the Perl Monks concerning the following question:

Obviously I can't just run a print statement on it, or I just get the hash value. I need to have the link assigned to a string so I can sort out a value that I need.

Thanks!

  • Comment on How do I return the text of a link after using find_link with WWW::Mechanize?

Replies are listed 'Best First'.
Re: How do I return the text of a link after using find_link with WWW::Mechanize?
by Your Mother (Archbishop) on Nov 01, 2005 at 04:51 UTC

    The links in WWW::Mechanize are WWW::Mechanize::Link objects. So you have to use their methods to get more than their auto-stringification.

    use strict; use WWW::Mechanize; my $mech = WWW::Mechanize->new(); $mech->get('http://amazon.com/'); $mech->success or die $mech->status(); my $link_obj = $mech->find_link (text_regex => qr/computer/i ); printf( "%s\n\t%s\n\n", $link_obj->text(), $link_obj->url(), );

      Thanks guys. I ended up with:
      my $topic_obj = $mech->find_link (text_regex => qr/[LINK TEXT]/i ); $PrevMsg = $topic_obj->url; $PrevMsg =~ s/[SORTED DATA]//g;
      and it works wonderfully.
Re: How do I return the text of a link after using find_link with WWW::Mechanize?
by bobf (Monsignor) on Nov 01, 2005 at 04:46 UTC

    The docs for WWW::Mechanize state that the find_link() method

    returns a reference to a two element array which has the link URL and link text, respectively. If it fails to find a link it returns undef.
    The returned value should be an array ref if it succeeded (not a hash as stated in your post). If you really are getting a hash ref, please post some code with example input and output so we can replicate it.

    You can do something like this:

    my $result = $a->find_link( text => "download" ); if( defined $result ) { my ( $url, $linktext ) = @{ $result }; # use $linktext here }

    HTH

    Update: This is true for WWW::Mechanize 0.48, the latest version that I found using PPM. The most recent version on CPAN (1.16) uses WWW::Mechanize::Link objects, as Your Mother indicated below.

Re: How do I return the text of a link after using find_link with WWW::Mechanize?
by spiritway (Vicar) on Nov 01, 2005 at 04:58 UTC

    Check out WWW::Mechanize::Examples.