raphael() wrote:
> Hi,
> 
> I am trying to understand WWW::Mechanize
> 
> I understand that the downloaded content is stored in content().
> Why am I not able to use a regex on it in scalar form?
> 
> ------code------
> 
> use strict;
> use warnings;
> use WWW::Mechanize;
> 
> my $mech = WWW::Mechanize->new();
> $mech->get("http://checkip.dyndns.org";);
> my $last_page = $mech->content(); # last page fetched
> 
> # this works if I store content in an array @last_page
> # for ( @last_page ) {
> #    if ( m/([\d+.]+)/ ) {
> #    print "$1\n";
> #    }
> # }
> 
> # ( my $ip ) = grep/(\d+\.)/, $last_page;
> 
> ( my $ip = $last_page ) =~ m/([\d+\.]+)/;
> print "$ip\n";
> 
> ------end------
> 
> my $ip gets the whole source page as its value.
> 
> --
> Got it while writing out this post :)
> --
> 
> Now the question becomes what is the difference between these two?
> 
> ( my $ip = $last_page ) =~ m/([\d+\.]+)/;
> 
> ( my $ip ) = ( $last_page ) =~ m/([\d+\.]+)/;
> 
> I think the above one is "wrong syntax" for using list context?
> 
> Also  how can I make grep work?
> 
> ( my $ip ) = grep/(\d+\.)/, $last_page;
> 

Try:

  my ( $ip ) = $last_page =~ m/([\d\.]+)/;

This will capture the first one.  To get more than one:

  my @ips = $last_page =~ m/([\d\.]+)/g;


grep() works on lists.  See `perldoc -f grep` for details.


-- 
Just my 0.00000002 million dollars worth,
  Shawn

Programming is as much about organization and communication
as it is about coding.

I like Perl; it's the only language where you can bless your
thingy.

Eliminate software piracy:  use only FLOSS.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to