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

I'm a bit confused... I wrote a script to test the Geo::Weather module, and it refuses to run. Then I cut and paste the script into the command line, and it runs fine. From the file, I get the error:
Can't use string ("-4") as a HASH ref
referring to this line:
print "The current temperature is $current->{temp} degrees\n";
It really does work if I type "perl -w" at the command line and then paste it in... What silly mistake am I making this time?
use strict; use Geo::Weather; my $weather = new Geo::Weather; my $current = $weather->get_weather('19067'); print "The current temperature is $current->{temp} degrees\n";

Replies are listed 'Best First'.
Re: I'm confused... using Geo::Weather
by Aighearach (Initiate) on Jun 25, 2001 at 22:06 UTC
    Your problem is probably that you're running on a unix system and don't have the hash-bang line correct. On my system I used:
    #!/usr/bin/perl -w use strict; use Geo::Weather; my $weather = new Geo::Weather; my $current = $weather->get_weather('19067'); print "The current temperature is $current->{temp} degrees\n";
    and it ran fine. 80 degrees. If you leave out the first line altogether, it will probably try to run it with /bin/sh, which gives odd errors when it sees Perl...
    --
    Snazzy tagline here
      No, I must've missed the first line when I pasted... regardless, when I cut-and-pasted your code (with the #!, and yes, /usr/bin/perl is the correct path on my system...) I still get that infernal error!
      Also, if I use $weather->report(); instead, I get an error from the perl module itself... The same hash ref error.

      Thank you for your time,
      Benjamin
        I get the same error when I run the code on a Win32 system. Using the code on the Aighearach's reply.

        --BigJoe

        Learn patience, you must.
        Young PerlMonk, craves Not these things.
        Use the source Luke.
        What is your perl version?
        --
        Snazzy tagline here
Re: I'm confused... using Geo::Weather
by bikeNomad (Priest) on Jun 25, 2001 at 21:59 UTC
    Well, that means that $current is -4. Which is probably an error indication of some sort from get_weather(). It's a bit peculiar to have a function return either a HASH ref or a negative number, though (I'd wonder about the sanity of the writer of Geo::Weather if that were the case).
      Thank you. As it happens, you are right to wonder...
      I'm still not sure exactly what was going on, but at least once the problem was when I requested get_weather('NY','New York');
      Apparently it really wants it in the format ('New York','NY') and instead of returning the ERROR_IN_QUERY message, it returned the ERROR_NOT_FOUND error code... all of the errors are negative numbers...

      Thanks again,
      Benjamin