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

Link subclass object method bug?
my @links = $mech->links; for( @links ){ print $_->text,$/ } __END__ results: Beatles - 01 - Miche..> Beatles - 02 - Drive..> Beatles - 03 - What ..> ...
whereas I wanted:
my @links = $mech->links; for( @links ){ print text($_->[0]).$/ } sub text { $_ = uri_unescape(shift) } __END__ results: Beatles - 01 - Michelle.mp3 Beatles - 02 - Drive My Car.mp3 Beatles - 03 - What Goes On.mp3


Replies are listed 'Best First'.
Re: WWW::Mechanize::Link bug?
by sanPerl (Friar) on Dec 23, 2005 at 05:15 UTC
    I Normally use following way to extract the URLs and it's realted Texts from $meach, and it works well as of now.
    my @links = $mech->links; foreach $href_links (@links) { #URL my $lnk = $href_links->url(); #Text my $txt = $href_links->text(); print $lnk."\t".$txt."\n"; }
    Would you be able to post the contents of the html page to PerlMonks? (Capture the event just before you run the foreach loop)
    print $mech->content
    , I am Not able to get clear pitcure as what output you want Vs What is there on the html page.
Re: WWW::Mechanize::Link bug?
by diotalevi (Canon) on Dec 23, 2005 at 03:57 UTC

    That's not subclassing. You violated the WWW::Mechanize::Link object's encapsulation and then called your own function on its guts.

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

      Obviously, I wasn't clear enough. The subroutine was given only to show what I wanted, not to imitate inheritance. I only compared the text method with text function to show the results.


Re: WWW::Mechanize::Link bug?
by ikegami (Patriarch) on Dec 23, 2005 at 05:50 UTC

    The text method returns what you see on the screen, which is Beatles - 01 - Miche..>. You want (part of) the URI, obtainable using the url method.

    print($_->url, $/) foreach $mech->links;
    A reply falls below the community's threshold of quality. You may see it by logging in.