in reply to Re^3: Pattern Matching Q?
in thread Pattern Matching Q?

I tried with the following code:
use strict; use WWW::Mechanize; use HTTP::Cookies; use HTML::TokeParser::Simple; my $mech = WWW::Mechanize->new( stack_depth => 1, agent =>"Mozilla/5.0 + (X11; U; Linux i68+6; en-US; rv:1.7) Gecko/20040918 Firefox/0.9.3 ") +; my $pic = "C:\/LV3.JPG"; $mech->cookie_jar( HTTP::Cookies->new(stack_depth => 1) ); $mech->get("http://tinypic.com"); if($mech->content() =~ /UPLOAD NOW/ ){ print "TinyPic loaded... "; $mech->form_name("upload_form"); $mech->field('the_file' => $pic); $mech->submit(); m{ <strong # match "<strong" .*? # skip to the next ( # (start capturing) http:// # "http://" .*? # skip to the next ) # (stop capturing) & # ampersand }x print $1; }
and it returned a syntax error. I also tried with the other version you gave me here:
use strict; use WWW::Mechanize; use HTTP::Cookies; use HTML::TokeParser::Simple; my $mech = WWW::Mechanize->new( stack_depth => 1, agent =>"Mozilla/5.0 + (X11; U; Linux i68+6; en-US; rv:1.7) Gecko/20040918 Firefox/0.9.3 ") +; my $pic = "C:\/LV3.JPG"; $mech->cookie_jar( HTTP::Cookies->new(stack_depth => 1) ); $mech->get("http://tinypic.com"); if($mech->content() =~ /UPLOAD NOW/ ){ print "TinyPic loaded... "; $mech->form_name("upload_form"); $mech->field('the_file' => $pic); $mech->submit(); m{<strong.*?(http://.*?)&}; print $1; }
This version didn't get a syntax error but also didn't output anything

Replies are listed 'Best First'.
Re^5: Pattern Matching Q?
by jrsimmon (Hermit) on Nov 27, 2007 at 22:25 UTC
    the matching operators work against the $_ default variable, unless otherwise specified. The easiest thing to do here is to load that variable with he contents that you want to search, preferably within a local block:
    my $url = undef; local{ $_ = $mech->content; #not sure if mech let's you do #this...basically you want to load #the $_ var with the data that holds #the url m{<strong.*?(http://.*?)&}; $url = $1; } print $url;
Re^5: Pattern Matching Q?
by madbombX (Hermit) on Nov 27, 2007 at 21:07 UTC
    The first version gives you a syntax error because you don't terminate the line (there is no semi-colon). The second version is syntactically correct, but my guess is that the $_ variable is not the one you want to be doing the regex on.

    I am not familiar enough with WWW::Mechanize to know how to get the returned content. You probably want to be searching something besides the implicit $_ that m{} uses.