Thank you 1nickt, I'm relieved that this did indeed need a tweak. Your suggestion works perfectly for the case that ARGV[0] actually matches some link. I made a more verbose example to show what happens when one letter is off. What seems counter-intuitive about this was what constituted success, and the output gave me half an idea why the original script was sending me down the rabbit hole of the .css link. It is the first link, and I can well imagine that default behavior goes that way when the regex fails. This is a bit verbose, so I'll use readmore tags:
C:\cygwin64\home\Fred\pages2\hunt>perl cpan6.pl HTML::Display success is 1 link is WWW::Mechanize::Link=ARRAY(0x29e1294) http://st.pimg.net/tucs/style.css?3 http://st.pimg.net/tucs/print.css http://search.cpan.org/uploads.rdf http://st.pimg.net/tucs/opensearch.xml / / /author/ /recent http://log.perl.org/cpansearch/ /mirror /faq.html /feedback /search?m=all&q=HTML%3A%3ADisplay&s=11 /search?m=all&q=HTML%3A%3ADisplay&s=11 /search?m=all&q=HTML%3A%3ADisplay&s=1&n=10 /search?m=all&q=HTML%3A%3ADisplay&s=1&n=20 /~corion/HTML-Display-0.40/lib/HTML/Display.pm /~corion/HTML-Display-0.40/ /~corion/ /~corion/HTML-Display-0.40/lib/HTML/Display/Common.pm /~corion/HTML-Display-0.40/ /~corion/ /~corion/HTML-Display-0.40/lib/HTML/Display/Mozilla.pm /~corion/HTML-Display-0.40/ /~corion/ /~corion/HTML-Display-0.40/lib/HTML/Display/Opera.pm /~corion/HTML-Display-0.40/ /~corion/ /~corion/HTML-Display-0.40/lib/HTML/Display/Dump.pm /~corion/HTML-Display-0.40/ /~corion/ /~corion/HTML-Display-0.40/lib/HTML/Display/Debian.pm /~corion/HTML-Display-0.40/ /~corion/ /~corion/HTML-Display-0.40/lib/HTML/Display/Phoenix.pm /~corion/HTML-Display-0.40/ /~corion/ /~corion/HTML-Display-0.40/lib/HTML/Display/Win32/OLE.pm /~corion/HTML-Display-0.40/ /~corion/ /~corion/HTML-Display-0.40/lib/HTML/Display/Win32/IE.pm /~corion/HTML-Display-0.40/ /~corion/ /~corion/HTML-Display-0.40/lib/HTML/Display/Win32.pm /~corion/HTML-Display-0.40/ /~corion/ /search?m=all&q=HTML%3A%3ADisplay&s=11 /search?m=all&q=HTML%3A%3ADisplay&s=11 http://www.yellowbot.com http://www.yellowbot.com success is 1 C:\cygwin64\home\Fred\pages2\hunt>perl cpan6.pl HTML::Displayz success is 1 Use of uninitialized value $link in concatenation (.) or string at cpa +n6.pl line 26. link is http://st.pimg.net/tucs/style.css?3 http://st.pimg.net/tucs/print.css http://search.cpan.org/uploads.rdf http://st.pimg.net/tucs/opensearch.xml / / /author/ /recent http://log.perl.org/cpansearch/ /mirror /faq.html /feedback http://www.yellowbot.com http://www.yellowbot.com Can't call method "url" on an undefined value at cpan6.pl line 28. C:\cygwin64\home\Fred\pages2\hunt>type cpan6.pl #! /usr/bin/perl use warnings; use strict; use 5.010; # work out the name of the module we're looking for my $module_name = $ARGV[0] or die "Must specify module name on command line"; # create a new browser use WWW::Mechanize; my $browser = WWW::Mechanize->new(); # tell it to get the main page $browser->get("http://search.cpan.org/"); # okay, fill in the box with the name of the # module we want to look up $browser->form_number(1); $browser->field( "query", $module_name ); $browser->click(); my $link = $browser->find_link( text_regex => qr{$module_name} ); my $success = $browser->success; say "success is $success"; say "link is $link"; $browser->dump_links; my $success2 = $browser->follow_link( url => $link->url ); $success2 = $browser->success; say "success is $success2"; C:\cygwin64\home\Fred\pages2\hunt>
It seems to me that the last place you want to be is stuck on the command line when you can't remember every proper keystroke in a module. I submit that testing whether $link is defined is worthwhile, and that one should get a window in either case. What's more, I found out that the case for failure in defining $link nevertheless turned up ranked, good alternatives. Furthermore, the 'mode' should be set to 'module' so that the behavior reflects the advertising. Now, I get no warnings or errors on the terminal, and ends up with a browser window that matched the regex or a ranked list of links that got close:
C:\cygwin64\home\Fred\pages2\hunt>perl cpan10.pl HTML::Display C:\cygwin64\home\Fred\pages2\hunt>perl cpan10.pl HTML::Displayz C:\cygwin64\home\Fred\pages2\hunt>type cpan10.pl #! /usr/bin/perl use warnings; use strict; # work out the name of the module we're looking for my $module_name = $ARGV[0] or die "Must specify module name on command line"; # create a new browser use WWW::Mechanize; my $browser = WWW::Mechanize->new(); # tell it to get the main page $browser->get("http://search.cpan.org/"); # okay, fill in the box with the name of the # module we want to look up $browser->form_number(1); $browser->field( "query", $module_name ); $browser->click(); my $link = $browser->find_link( text_regex => qr{$module_name} ); # make sure $link is defined if ( defined $link ) { my $success2 = $browser->follow_link( url => $link->url ); my $url = $browser->uri; system( 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe +', $url ); } else { $browser->back; $browser->submit_form( form_number => 1, fields => { query => $module_name, mode => 'module', }, ); my $url = $browser->uri; system( 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe +', $url ); }
Anyways, that's what I can figure out tonight. Web automation is truly fantastic.
In reply to Re^2: Using example script correctly for opening cpan module
by Aldebaran
in thread Using example script correctly for opening cpan module
by Aldebaran
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |