Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

problems passing values to subroutine using LWP::Useragent
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;


Report this thread to moderator Post Follow-up to this message
Old Post
shadkeene@hotmail.com
08-24-07 12:00 AM


Re: problems passing values to subroutine using LWP::Useragent
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

Report this thread to moderator Post Follow-up to this message
Old Post
Tom Phoenix
08-24-07 03:01 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:51 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.