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

This node was taken out by the NodeReaper on Tue Jun 21 11:09:44 2005 GMT

Replies are listed 'Best First'.
Re: Real Basic Perl OOP Problem
by tlm (Prior) on Jun 21, 2005 at 10:19 UTC

    If I'm not mistaken, you've asked this question before. The problem is with the line

    my $name2 = Message->name( $message_obj );
    This effectively invokes the name method with two arguments: the string 'Message' and the blessed reference $message_obj. The error happens when the function attempts to use the first argument (in this case, the string 'Message') as if it were a hash reference. What you want (I think) is
    my $name2 = $message_obj->name;
    Try that.

    Update: Just to clarify. I don't want to give you the impression that invocations of the form Class_Name->method are always wrong. If method is a class method, that's exactly what you should do. In your example, new is a class method (and therefore Message->new(...) is correct), whereas name is an instance method. Hence the difference in the way they are invoked.

    the lowliest monk

Re: Real Basic Perl OOP Problem
by BrowserUk (Patriarch) on Jun 21, 2005 at 10:22 UTC

    Why are you calling instance methods as class methods?

    You create an instance, called $message_obj here:

    my $message_obj = Message->new( $name, $subject, $message );

    But then you call the name() method as a class method, passing the object instance as a parameter?

    my $name2 = Message->name( $message_obj );

    That should be

    my $name2 = $message_obj->name();

    That's not to say that it will fix your problem, as the two are notionally equivalent, but you should be letting Perl take care of passing the instance handle for you. Why have a dog and bark yourself?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.