Code Comments
Programming Forum and web based access to our favorite programming groups.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;
Post Follow-up to this messageOn 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
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.