pip has asked for the wisdom of the Perl Monks concerning the following question:

Hello, Dear fellow Monks This is my 2nd question regarding this script, which originally didn't run because of an unavailable perl module UserAgent.pm. My webhost has now, with a lot kicking and screaming, installed this module. Unfortunately the script still doesn't run. Question: Could there be anything wrong with the script which, on another server, ran flawless? (there are no longer any fault messages displayed by carp, unlike before, with the perl module "saga".
#!/usr/bin/perl use CGI::Carp qw(fatalsToBrowser); open(W, "weather.js"); @lines = <W>; close(W); $out = join('', @lines); if (scalar(@lines) < 3 or -M "weather.js" > 0.25) { use LWP::UserAgent; $ua = LWP::UserAgent->new(); $ua->timeout(60); $request = HTTP::Request->new('GET', 'http://www.bom.gov.au/cgi-bin/wr +ap_fwo.pl?IDS10034.txt'); $res = $ua->request($request); if ($res->is_error) { $str = $res->message; } else { $str = $res->content; } $str =~ s/^.*IDS1003401//s; $str =~ s/HEADLINE.*?$//s; if ($str =~ /:\s+(.*?)\n\n/s) { $adeldesc = $1; $adeldesc =~ s/\n//g; } if ($str =~ /Adelaide City\s+(.*?)\s+Max\s+(\d+)/s) { $adelmax = $2; } if ($str =~ /Noarlunga\s+(.*?)\s+Max\s+(\d+)/s) { $norlmax = $2; } $out =<<EOF; var adeldesc = "$adeldesc"; var adelmax = "$adelmax"; var norlmax = "$norlmax"; EOF open(W, ">weather.js"); print W $out; close(W); } print <<EOF; Content-Type: text/x-javascript Pragma: no-cache Cache-Control: no-cache Expires: -1 $out EOF

Replies are listed 'Best First'.
Re: weather script not working
by Vautrin (Hermit) on Feb 13, 2004 at 02:21 UTC

    If the version of LWP::UserAgent that was installed does something differently then the version of UserAgent you originally used, the answer is, yes, it could have broken something. However, I've noticed you don't have:

    use strict; use warnings;

    At the top of your script. So I ran your script and added those two lines and got the following:

    Now all those errors mean that you're not declaring before using, or that you're not explicitely accessing (i.e. Package::sub instead of sub), and this isn't necessarily the source of your errors. But it's very good practice to use warnings; use strict; at the top of your code because it helps unmask errors.

    I would recommend taking the time to go through your script to make it compatible with use strict; use warnings; That may help isolate the problem.

    Update: I was thinking about it, and you can compact the following lines of code:

    $request = HTTP::Request->new('GET', 'http://www.bom.gov.au/cgi-bin/wr +ap_fwo.pl?IDS10034.txt'); $res = $ua->request($request);

    Into:

    And not to nit pick, but you should always double check your content type. Otherwise you might accidently get a JPEG or something, i.e.:

    if ($res->is_error) { $str = $res->message; } else { $str = $res->content; }

    Under my example would become:

    Of course, the updates were stylistic and a matter of preference (except for checking content type unless you are absolutely sure you can only get the proper content type). TMTOWTDI


    Want to support the EFF and FSF by buying cool stuff? Click here.
      Hello Vautrin, Thanks for your help, as I was replying to Abigail, the script worked only once tonight, to my surprise, but then stopped to work when I wanted to execute it again, as it didn't display the 2nd city I would have liked to display. I tried putting your suggestion of changes to the code in their place, but no success, due to the Internet Explorer error message.
Re: weather script not working
by Abigail-II (Bishop) on Feb 13, 2004 at 00:06 UTC
    So, what error messages do you get? And why are you convinced your open() succeeds?

    Abigail

      Hello Abigail, The error message is "Internet Explorer cannot download weather.pl from www.server.net." The strange thing was, when I tried to execute the script tonight, it worked, and displayed the details on screen, but when I tried it again 5 minutes later it came up with this error message.
Re: weather script not working
by hossman (Prior) on Feb 13, 2004 at 19:02 UTC

    Abigail is right ... you sould allways check the result of a call to open. I suspect that either your script isn't being run in the same working directory as your weather.js file, or your script isn't being run by a UID that has permission to open that file -- especially on that open to write call.

    PS: You realize you have a "+" in the URL you are trying to fetch -- you probably dont' want that there, that URL doesn't seem to work unless you delete it.

      Hello Hossman, Thanks for your help. As I wrote on my previous replies, your suggestion about an invalid UID may explain the error code from IE. But why did it run once, I wonder. Where do I need to have the UID set. Would the web host do that, and where is this set? I have seen a few "+" in the code, which one need to be removed? Thanks again for your input, and sorry about all those questions. pip