For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2007 > problems passing values to subroutine using LWP::Useragent









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author problems passing values to subroutine using LWP::Useragent
shadkeene@hotmail.com

2007-08-23, 7:00 pm

Hi,
I'm back for some advice from you helpful folks. I'm pulling weather
data from the web and manipulating it...using airport codes for
various weather data, and I use the same subroutine multiple times for
various airports. I've had some success passing various airport codes
to the other subroutines using functions like lwp::simple, but in this
one it's not working. The actual subroutine works by itself when I add
the airport identifier and take out the Sub { header. But when I use
it as a subroutine and pass an airport identifier into the web
address...which should bring up the data for that airport...it returns
nothing...no error message either. I'm fairly new to using
lwp::useragent, so it may be that I'm using it as a subroutine
incorrectly.

Here's the code if you can lend some help...thanks!
#!/perl/bin/perl -w
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use strict;
use DBI;
use LWP::Simple;
use HTML::TokeParser::Simple;
my $SLPdata=();





print header;
print start_html("Gradients");






my $SLP = FcstPress("sfo");
print "$SLP";

sub FcstPress {
my $url = 'http://68.226.77.253/text/NAM80km/NAM_k@_.txt';
#substitutes passed variable into @_ like ksfo.txt
use LWP::UserAgent;
my $browser = LWP::UserAgent->new();
my $res = $browser->get($url) or die "Error getting file: $!";
my @lines = split('\n' ,$res->content); #splits on each new line
foreach (@lines) {
if ($_ =~ m/^ Mean/i) { #finds line with pertinent data
my @SLPdata = split(' ',$_); #splits on each whitespace
print $SLPdata[5];
}
}
}
print end_html;

Tom Phoenix

2007-08-23, 10:01 pm

On 8/23/07, shadkeene@hotmail.com <shadkeene@hotmail.com> wrote:

> I've had some success passing various airport codes
> to the other subroutines using functions like lwp::simple, but in this
> one it's not working.


Has anyone suggested using higher-level WWW::Mechanize instead of
low-level LWP::Simple? It's not the same as buying butter instead of
milk and a churn, but it's a step in the right direction for many
applications. (Maybe yours, maybe not.)

> use CGI::Carp qw(warningsToBrowser fatalsToBrowser);


I hope that you comment-out or remove that line when you're done
developing your code. Otherwise, someone who discovers a flaw in your
program gets to see a helpful error message explaining what they did
wrong, and can all the more easily use that flaw to break in to your
system.

> sub FcstPress {
> my $url = 'http://68.226.77.253/text/NAM80km/NAM_k@_.txt';
> #substitutes passed variable into @_ like ksfo.txt


Your e-mail application (or something along the way) seems to have
lost the indentation for your program; every line starts at the left
margin on my screen. This part is clear: The comment is a lie, since
variables don't interpolate into single-quoted strings. But even if it
used double quotes, it works only for exactly one subroutine argument,
doesn't it? There may be a better way.

my $tla = shift @_;
die "Unexpected tla: '$tla'" unless $tla =~ /^\w+$/; # maybe
# Here's double-quote interpolation for you:
my $url = "http://68.226.77.253/text/NAM80km/NAM_k$tla.txt";

If @_ contains more than one item, your subroutine could perhaps
return a list of responses. Or it might be better to die with a
message about bad usage, depending upon your situation.

Hope this helps!

--Tom Phoenix
Stonehenge Perl Training
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com