in reply to Unblessed Reference Message

See the comments. A ref becomes an object after being blessed, as now Perl knows who/what it is.
ap.pm: package ap; sub new { my $self = {}; bless $self; #comment this will cause the error you described. return $self; } sub item_id { my $self = shift; if (@_) { $self->{ITEM_ID} = shift; } return $self->{ITEM_ID}; } 1; ap.pl: use ap; use strict; my $a = new ap; $a->item_id(10); print $a->item_id();


Update

So your code is more simple than I thought, there is no OO involved.

But the underlying mistake looks the same to Perl. As your syntax made Perl thought it was a method call, but the host is an unblessed ref.

Replies are listed 'Best First'.
Re: Re: Unblessed Reference Message
by converter (Priest) on Jan 26, 2003 at 21:36 UTC

    A ref becomes an object after being blessed, as now Perl knows who/what it is.

    This is incorrect, and will eventually bite you in the rear end. From perlfunc:

    bless REF,CLASSNAME bless REF This function tells the thingy referenced by REF that it is now an object in the CLASSNAME package...

    It is the thing referred to, not the reference, that is blessed. This is why we can copy the reference returned by bless and still invoke methods against the reference--perl knows that the reference refers to an object of class class because it's the referent that is marked as belonging to a particular package.

      Thank you for making what I said more formal, but to be honest, I mean exactly the same thing, and I didn't expect people to misunderstand it. (Or to be precise, you correctly understood what was on the screen word by word, but had the wrong expectation.)

      This is a good lesson, and I will try to make my wording more formal.
Re: Re: Unblessed Reference Message
by b310 (Scribe) on Jan 26, 2003 at 21:18 UTC
    Thank you all. Problem eliminated.