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

I'm getting this blasted error: Can't use string "MyModule" as a HASH ref while "strict refs". I found this: http://www.troubleshooters.com/codecorn/littperl/perlfuncorder.htm, but I couldn't find much else and my subroutines are not prototyped (in fact, this is a module I'm writing, and it is failing on the method new). Here's the general gist of the code: the new method goes like:
sub new { my $self = shift; bless... my $dbh = shift; $self->{dbh} = $dbh; return $self; }
then, in another file:
use MyModule; use DBI; my $dbh = DBI->connect(...); my $foo = MyModule::->new($dbh);
both files are use strict; Can anyone help me out with what is going on here? I even tried passing the $dbh by reference, but it gave me the same error.

Edit kudra, 2002-01-10 Appended to title

Replies are listed 'Best First'.
Re (tilly) 1: Can't use string
by tilly (Archbishop) on Jan 09, 2002 at 08:00 UTC
    The problem is that in your new method, $self is the name of the class and not an object. Try this:
    sub new { my $self = bless {}, shift; $self->{dbh} = shift; # etc return $self; }
    An incidental point. You should pick a consistent indentation style with an indent in the range 2-4 characters and stick with it. It significantly improves ease of comprehension. Steve McConnell's excellent book Code Complete has a lot more on this and other issues, I highly recommend it (even though it is not about Perl).
Re: Can't use string
by jarich (Curate) on Jan 09, 2002 at 08:25 UTC
    I can see two problems. Change:
    my $foo = MyModule::->new($dbh);
    unless that's just a typo, into
    my $foo = MyModule->new($dbh);
    Also as was mentioned previously, you probably want to change your new function to start like:
    sub new { my $class = shift; my $self = {}; # self is a hashref. bless $self, $class; .... }
    because the very first thing your new function gets passed when you call it as MyModule->new(), is a scalar containing the string "MyModule". You can bless that scalar into any class you might like, but you can't magically make it turn into a hashref. Without reassigning it anyway.
      Sorry, I'm such a retard. Mistake copying a method to the constructor. Anyway, to address two things: sorry for the indentation-- i do indent! i was just lazy and typing in from memory. more importantly-- $obj = MyModule::->new; is perfectly valid, although ugly; (pg 316 of Camel has a great bit). Basically, it says that: $obj = MyModule->new; if fine most of the time, but could be confusing if MyModule is the same of a subroutine (unlikely, I know), which would parse as $obj = MyModule()->new();, where the extra :: is guarenteed to parse correctly.
        I stand corrected. Thanks.
Re: Can't use string
by jonjacobmoon (Pilgrim) on Jan 09, 2002 at 09:59 UTC
    Others have answered your question here, so I won't repeat; however. I will add that you might want to look up Damian Conway's book, Object Oriented Perl, published by Manning Press. Also, the tutorials here are excellent.

    Object Orient Perl Book
    Perl Tutorial (also perldoc perltoot on the command line)


    I admit it, I am Paco.