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

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

by Nocturnus (Beadle)
on Apr 22, 2012 at 06:55 UTC ( [id://966439]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^3: Error: Can't use string as a hash ref
by aaron_baugher (Curate) on Apr 23, 2012 at 02:05 UTC

    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://966439]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-03-19 06:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found