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

Oh great and wise monks, I am currently working on developing a script using the Perl Module WWW::Mechanize, I have gotten some of the things I need working, however I am running into an issue, and I am unsure how to resolve it. I have searched through the documentation as well as several good searches trying to figure out where I am going wrong to no avail. I am hoping that someone will see the mistake I am making.

I am using Mechanize to navigate to a webpage and run a search, that part works perfectly fine. However, when I finish my search, I want to go and retrieve a list of all the links on the new page, and be able to go through each link it finds to another page to retrieve information. Currently, to my knowledge, I am getting the list/array as I want, but I am unable to access it in the fashion I need. Instead, I normally get nothing printed to STD_OUT when taking in these links as an array, and I get a reference to an array printed to STD_OUT if I have it stored in a scalar. The problem I need is to be able to access this reference, but I am unable to figure out how to do that.

I have tried several things, but nothing has currently worked, I am currently just trying to get it to the point of printing the contents of the generated array to STD_OUT hence the foreach loop at the end of Search.

#!/usr/local/bin/perl require WWW::Mechanize; use strict; use warnings; { my $mech = WWW::Mechanize->new(); my $Input = ("LastName, FirstName"); &Search; sub Search { #print "Enter Search String:"; #goes to main search page. $mech->get('mywebsite.com/search.asp'); print "Webpage navigation start success result: "; &Get_Success; #submits the search form on the webpage just entered. $mech->submit_form ( form_number => 1, fields => {query => $Input, }); print "Form submission success result: "; &Get_Success; #finds all the links with "Server-" found inside the title. my @temp1 = $mech->find_all_links( text_regex => qr/Server-/i); print "Link retrieval success result: "; &Get_Success; foreach (@temp1) { print $_->url(), "\n"; print $_->text(), "\n"; print $_->name(), "\n"; print $_->tag(), "\n"; } sub Get_Success { #prints the success result and starts a new line. print $mech->success(); print"\n"; }

I have looked through these locations, and have found nothing I can see that helps me, perhaps I am just misunderstanding this information and how to use it. Any help is appreciated.

http://search.cpan.org/dist/WWW-Mechanize/lib/WWW/Mechanize.pm#LINK_METHODS

http://search.cpan.org/~jesse/WWW-Mechanize-1.72/lib/WWW/Mechanize/Link.pm#___top

http://forums.devshed.com/perl-programming-6/www-mechanize-link-array-reference-to-plain-english-276590.html

http://www.justskins.com/forums/mechanize-links-problem-49681.html

  • Comment on Help Getting WWW::Mechanize Link reference array to output to STD_OUT.
  • Download Code

Replies are listed 'Best First'.
Re: Help Getting WWW::Mechanize Link reference array to output to STD_OUT.
by zentara (Cardinal) on Jul 03, 2012 at 17:48 UTC

      So first off, sorry, I forgot to run a search, I am normally more on top of things like that :).

      I checked out both links, and of the two, Fetching links with WWW::Mechanize seemed to be the one relevant to me as WWW::Mechanize and following multiple links results in the problem of getting bad links.

      In Fetching links with WWW::Mechanize, the OP, was having the same issue I am, however I have tried the solutions in the two posts there already. I have both searched through WWW::Mechanize::Link and have tried to use the built in functions (as seen in the foreach loop) as well as the other post using a similar foreach loop that I have, (I have tried that foreach loop as well, to make sure that it was not just the loop.) Perhaps I am using the reference incorrectly?

        Perhaps I am using the reference incorrectly?

        No, not really. While you're not coping with scoping you're using find_all_links... correctly

        Try it yourself

        $ mech-dump --links http://example.com /_css/2008.1/reset-fonts-grids.css /_css/2008.1/screen.css

        What version of mechanize do you have?

        Upgrade upgrade upgrade