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

Hello! My question to you, blessed with the knowledge of perl is: Can the following script, used to display weather info, be altered, so it does not require the useragent.pm library? I have changed servers, and this module is not available.
thank you all
#!/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 without UserAgent.pm ?
by samtregar (Abbot) on Feb 10, 2004 at 23:34 UTC
    Yes, but it probably won't be easy. In this script LWP is being used to request data from another CGI script at http://www.bom.gov.au. If you want to replace LWP you'll have to write your own web-page grabber using basic socket commands, by calling an external process like "wget" or by some other means.

    I think it would be easier to just install LWP local to your script. For example, if you installed LWP into /home/pip/modules, you could put this line at the top of your script:

        use lib '/home/pip/modules';

    -sam

      Hi Sam, Thanks's for the reply. I know that my server has supplied me with a "includes" directory, but I just can't get my head around getting that module installed, despite looking at instructions at c-pan. pip
        I think you're going to have to roll up your sleeves and give it a try. If it doesn't work, post a new question showing what you did, explaining what you expected to happen and showing what actually did happen.

        Here's a hint: the end result of your efforts should be a bunch of *.pm files laid out in a series of directories which match their names. So you'll see "LWP.pm" and a subdirectory called "LWP" containing "UserAgent.pm" and so on. When you've got that all you need is to point Perl at it with "use lib" and your script should work again.

        -sam

Re: weather without UserAgent.pm ?
by Abigail-II (Bishop) on Feb 10, 2004 at 23:42 UTC
    Well, of course. HTTP is quite a simple protocol, so it shouldn't be too hard. Just create a socket, write your request on the socket, read the response, and close the socket. After reading the response, split the response into a headers part and a payload. Check the headers to see if you got a proper response, and then off your go with the payload.

    Abigail

      Hello Abigail, Thanks for your reply to my question. Would you be able to give some more details to your response, perhaps an example? I am just too much of a novice. You may think I shouldn't touch those things in the first place, which may be right, but it would be nice to get this script going again.
        Would you be able to give some more details to your response, perhaps an example? I am just too much of a novice.
        Eh, no, I will not. It's going to take me too much time. If you want to handicap your coding by excluding the use of modules, than that's fine. But don't come and ask me to do the work for you.

        Abigail

Re: weather without UserAgent.pm ?
by scottj (Monk) on Feb 11, 2004 at 00:01 UTC
    Another option would be to simply download LWP, install it locally in your own lib directory, and then tell the script where you've installed it. This is probably the easiest approach to making your script work.
      Hi Scott, Thank you for your reply too, as I mentioned in the reply to Sam, I am very much of a novice, when it comes to scripting, and I am happy when I get things working "out of the box", and admire people with the skill to write these programs.