Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

use of strict ....

by perlknight (Pilgrim)
on Nov 05, 2006 at 01:28 UTC ( [id://582277]=perlquestion: print w/replies, xml ) Need Help??

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

All, I have this line:
use strict; use lib "/usr/local/nagios/bin/libexec"; use utils qw(%ERRORS); .... exit ERRORS{"CRITICAL"};
strict will puke if I use ERRORS{"CRITICAL"} since I have not declared it with the my pragma. Is there a better way of using
use utils qw(%ERRORS)
without triggering strict. Thanks.

Replies are listed 'Best First'.
Re: use of strict ....
by rhesa (Vicar) on Nov 05, 2006 at 01:39 UTC
    You probably meant to write
    exit $ERRORS{"CRITITICAL"};
    strict helps you :)
Re: use of strict ....
by holcapek (Sexton) on Nov 05, 2006 at 09:16 UTC
    You declared (or whatever use utils did) a hash called %ERRORS.

    Yet later in the code, though you would like to get value of key CRITICAL of that hash, you used name ERRORS in this way and didn't know about it:

    {"CRITICAL"} is resolved as a block that returns value of its last expression, i.e. string "CRITICAL"; this value is considered to be a class name, and ERRORS as a method from this class; so, you're calling method ERRORS of class CIRITCAL, what would be rewritten like CRITICAL->ERRORS();.

    Hope this haven't confused you too much :-)

    Perl on.
      Than why would it work if I took the "use strict" pragma out? BTW, it's a typal, it should be "CRITICAL". I took a look at utils.pm, it's a hash, not a method. To clarify, you saying I should use single qoute:
      exit $ERRORS{'CRITICAL'};
      instead of double qoute:
      exit $ERRORS{"CRITICAL"};
      because it intrepret the double qoute as:
      $CRITICAL->ERRORS();
        It doesn't matter if you use single, double, or no quotes. any of these are fine.
        $ERRORS{CRITICAL} $ERRORS{'CRITICAL'} $ERRORS{"CRITICAL"}

        The error was that you forgot the '$', so perl was treating ERRORS as a subroutine invocation, and passing the hashref {"CRITICAL" => undef} as the argument.

        You can verify that behaviour like this:

        use Data::Dumper; sub ERRORS { print "ERRORS called\n"; print Dumper(\@_); } $a = ERRORS{"CRITICAL"};
        Which outputs:
        ERRORS called $VAR1 = [ { 'CRITICAL' => undef } ];
        By the way - in the future instead of saying "strict will puke" it would be better to provide the error.
Re: use of strict ....
by johngg (Canon) on Nov 05, 2006 at 11:34 UTC
    rhesa has given you a pointer to what may be the problem but I notice that you have what looks like a typo or cut'n'paste error in your code. You have put "CRITITICAL" rather than "CRITICAL" which probably won't help matters.

    Cheers,

    JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://582277]
Approved by shenme
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-04-23 08:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found