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

Hello Monks,

I'm new to Perl. I received the following message while running a script.

Can't call method "item_id" on unblessed reference
Can someone give me some advice on how to eliminate this message?

Thanks.

update (broquaint): added formatting

Replies are listed 'Best First'.
Re: Unblessed Reference Message
by pg (Canon) on Jan 26, 2003 at 21:12 UTC
    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.

      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.
      Thank you all. Problem eliminated.
Re: Unblessed Reference Message
by castaway (Parson) on Jan 26, 2003 at 20:56 UTC
    1. try 'use warnings;' and 'use diagnostics;' at the top of your code and then see what it says.
    2. try posting your code here and telling us which line produces the error?
    C.
      Hello, I tried 'use warnings' and 'use diagnostics' and received the same message. Here's part of the code:
      sub get_product_table { my $sth = shift; my @row; while (my $ref = $sth->fetchrow_hashref ()) { my $serve_url = sprintf ("serve_image.cgi?item_id=%s", escape ($ref->{item_id})); # generate a form allowing a quantity of the item to be added # to the cart push (@row, start_form(-method=>'GET', -action=>url()), hidden( -name => "choice", -override => 1, -default => "add" ), hidden( -name => "item_id", -override => 1, -default => escapeHTML( $ref->{item_id} )), Tr ( print img ({-src => "$serve_url;thumbnail=1", -alt => $ref->item_id +}), td ( escapeHTML( $ref->{item_id} )),
      I believe the line that is causing the problem is the line that starts with "print img". Thanks
        And you are correct.
        You wrote '$ref->item_id' when you probably meant '$ref->{item_id}', it's trying to call item_id as a function/method..
        C.
Re: Unblessed Reference Message
by virtualsue (Vicar) on Jan 26, 2003 at 21:04 UTC
    Can you give us some more information and show us the relevant portion of the script?

    Update: I believe that

    ...$ref->item_id}...
    should be
    ...$ref->{item_id}...
    in the line you identify as being at fault. Make sure you add a curly brace on either side of 'item_id'