Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Error: Can't use string as a hash ref

by AnomalousMonk (Archbishop)
on Apr 21, 2012 at 17:39 UTC ( [id://966392]=note: print w/replies, xml ) Need Help??


in reply to Error: Can't use string as a hash ref

An error message you are not getting suggests you do not have strictures enabled; had they been, you might have been pointed down the Right Way sooner. IMO, always a good idea to enable strictures – and warnings! (Example code run under Strawberry 5.10.1, but should perform identically under any version.)

>perl -w -le "my @ra = (X => Y); print qq{@ra}; " X Y >perl -wMstrict -le "my @ra = (X => Y); print qq{@ra}; " Bareword "Y" not allowed while "strict subs" in use at ... Execution of -e aborted due to compilation errors.

Replies are listed 'Best First'.
Re^2: Error: Can't use string as a hash ref
by Nocturnus (Beadle) on Apr 22, 2012 at 06:55 UTC

    At first, thanks for the hint.

    Of course, all of my scripts usually start with

    #/usr/bin/perl -w use strict;

    But I have stripped out as much as possible to boil the example down to the really important things. In this case, the important thing is that the occurrence of an error message does not depend on using strictures or not (and thus the many documents I found on the web don't apply to my problem), but on the

    use base ...

    line. So I got an error message even without using strictures.

    But you are right in that the error message changes if I put a

    use strict;

    at the beginning of the script. The message is then:

    Bareword "MyHandler" not allowed while "strict subs" in use at test.pl line 5.
    Execution of test.pl aborted due to compilation errors.

    Unfortunately, that wouldn't have helped me either. I still wouldn't have known how to solve the problem. But the comment of choroba pointed me to the right direction (see other comment).

    Thank you very much again,

    Nocturnus

      But adding use strict to your own script made it tell you your actual problem (whether you understood the error message or not), rather than letting it fall through to cause an error in the module. The problem wasn't with OO or your module; it was your use of the "bareword 'MyHandler'", as it says.

      When you use a bareword (a bit of text without a sigil or quotes or anything else to give it context) in a place where barewords aren't specifically allowed (like hash keys or file descriptors), Perl (in non-strict mode) does its best to guess what you mean by it. In most cases, including this one, it turns the bareword into a string, here coming up with "MyHandler", which it happily passes as the argument to your module. Then the module finally chokes on it because it expects a hash (object) reference. Using strict warns you sooner, at the exact point of the problem.

      Aaron B.
      My Woefully Neglected Blog, where I occasionally mention Perl.

        Thank you very much for the explanation. It is very useful, and I have learned a lot from it.

        Regards,

        Nocturnus

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-24 07:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found