in reply to Re: Retrieving HTML with LWP::UserAgent
in thread Retrieviing HTML with LWP::UserAgent
This reply is very impressive.
Just to extend a little bit on this. There are two ways, we might call this sub. We either call it before it is defined, or call it after it is defined. Let's look at both of them:
Call before it is defined.
use LWP::UserAgent; use strict; use warnings; print retrieve_url( 'http://www.finasta.lt' ); sub retrieve_url() { my $url = shift; my $ua = LWP::UserAgent->new(); my $res = $ua->get( $url ); if ( $res->is_success() ) { my $content = $res->content(); $content =~ s!\A\s+!!; return( $content ); } else { die( "retrieval error: ", $res->status_line() ); } }
In this case, you would be warned that:
main::retrieve_url() called too early to check prototype at a.pl line +6.
However, Perl will close one eye and let the code run "successfully".
Call after the sub is defined. If you want to get rid of that annoying warning, you have to define the sub first, then Perl will stop the program from running, which is ideal to me.
use LWP::UserAgent; use strict; use warnings; sub retrieve_url() { my $url = shift; my $ua = LWP::UserAgent->new(); my $res = $ua->get( $url ); if ( $res->is_success() ) { my $content = $res->content(); $content =~ s!\A\s+!!; return( $content ); } else { die( "retrieval error: ", $res->status_line() ); } } print retrieve_url( 'http://www.finasta.lt' );
Try it, and you get:
Too many arguments for main::retrieve_url at a.pl line 23, near "'http +://www.fi asta.lt' )" Execution of a.pl aborted due to compilation errors.
|
|---|