in reply to Re^3: Help Getting WWW::Mechanize Link reference array to output to STD_OUT.
in thread Help Getting WWW::Mechanize Link reference array to output to STD_OUT.

I am currently using WWW::Mechanize version 1.72 (read: latest version).

I will check out the link you provided, and return with an update on my findings, thanks for the help!

UPDATE: so after looking through the links provided above, I am rather confused by what you mean by saying "While you're not coping with scoping you're using find_all_links... correctly" I have looked, and my scope appears to be within the same block, and as such should be able to see it during execution time, which yields true, if you take the return of find_all_links and place it in a scalar, as it returns a reference to an array. So perhaps I am just blind, but I am missing what you mean. Would you mind expanding on what you mean, in an effort to help me understand?

  • Comment on Re^4: Help Getting WWW::Mechanize Link reference array to output to STD_OUT.

Replies are listed 'Best First'.
Re^5: Help Getting WWW::Mechanize Link reference array to output to STD_OUT.
by Anonymous Monk on Jul 03, 2012 at 21:06 UTC

    Yup, I meant to link to the Tutorials category Variables and Scoping

    Function which don't take arguments are bad

    Your original code perltidyd

    <c>#!/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"; } }
    </c>

    How to write it so its coping with scoping :)

    sub Main { my $mech = ...; ...; Get_Success( $mech ); ...; } sub Get_Success { my( $mech ) = @_; #prints the success result and starts a new line. print $mech->success(); print "\n"; }

    Or better still, use the existing features of Mechanize

    #!/usr/local/bin/perl -- use strict; use warnings; use WWW::Mechanize 1.72; Main( @ARGV ); exit( 0 ); sub Main { my $search = shift; my $mech = WWW::Mechanize->new( qw/ autocheck 1 show_progress 1 /); $mech->get( $search ); my @serverLinks = $mech->find_all_links( url_regex => qr/_css/i ); for my $link( @serverLinks ) { for my $member ( qw/ url text name tag / ){ no warnings 'uninitialized'; print $link->$member, "\n" ; } } } __END__ $ perl dumplinks http://nowhere.example.com ** GET http://nowhere.example.com ==> 500 Can't connect to nowhere.exa +mple.com:80 (Bad hostname) (1s) Error GETing http://nowhere.example.com: Can't connect to nowhere.exam +ple.com:80 (Bad hostname) at dumplinks line 15. $ perl dumplinks http://example.com ** GET http://example.com ==> 302 Found ** GET http://www.iana.org/domains/example/ ==> 200 OK /_css/2008.1/reset-fonts-grids.css link /_css/2008.1/screen.css link /_css/2008.1/print.css link

    Also error is  my $Input = ("LastName, FirstName"); you can see how if you use Basic debugging checklist

      So I took your suggestions here, and first off, thanks for the help, I also have been able to progress forward, kinda.

      I have changed how I create the Mechanize object to provide autocheck and to show_progress both set to true. It provided me with some other insight that I have worked through and resolved.

      However, I have hit the same problem, I have even tried dereferencing the array created, to know avail. I still receive the error of the array or variable being unintialized. I have attempted this by attempting to print it via the previous loop, and by providing this line taken from your code.

      my $temp1 = $mech->find_all_links( text_regex => qr/Server-/i ); my @temp2 = shift $temp1; print "@temp2\n";
      Perhaps I am just missing something super obvious, since I appear to be in the correct scope, and I am trying to use the one created, even though it appears not feasibly possible. Any other suggestions?

        Any other suggestions?

        Study my code closer and employ Basic debugging checklist item 4 ( Dumper )

        That part of your original code was right (just like mine). I add  no warnings ... in mine, because the CSS links I'm selecting don't have text/name attributes (returns undef), which warnings will issue a warning for. If the links you're selecting also don't have some attributes, they'll be undef, and you'll get warnings when you try to print them. If you don't it, either disable the warnings (in smallest possible scope) or code-around it

        my $value = $link->$member; defined $value and print "$value\n";

        If it is something else, you'll have to Dumper-up a minimal demonstration , maybe return value of find_all_links or $mech itself ( How do I post a question effectively? )