in reply to bless array of hashes correct get method wrong

You mixed things between dereferencing the object reference (my @item = @$itemref;) and calling the object method ($item[0]->get_desc()). The former is just fine for the derefencing sake, but for the latter, you use $item[0] as object to call the method. But $item[0] is not an object, it's just an array element in HASH reference. So, it's true that $item[0] is a reference, but it's unblessed, so you can't use it to call a method, hence the error: Can't call method "get_desc" on unblessed reference.

You should call it with $itemref, this is the variable holds the object. Even if this was the case, you would get error because in your method definition, you use an array reference but dereference it as HASH.

The $_[0] is the first element of @_, which holds parameters passed in to methods/subroutines. In a method, $_[0] is supposed to be the object itself, hence the use of $self as a convetional variable name.

I assume that you (hopefully) have a sort of this structure:

# simplified version my @array = ({id=>1, name=>'Linux'}, {id=>2, name=>'Perl'}); my $self = [@array]; bless $self, $class

Remember, the first argument to method is the object itself. Your object is a reference to an ARRAY so you can't dereference it as HASH. Instead, you want something like this.

sub get_desc { my($self, $idx) = @_; return $self->[0]{_description}; }

And you call it like this,

my $item = Item::Item->new('some record id'); print $item->get_desc(0);

You should stay with your object after creating it, don't dereference to use it to call an object method.

my $itemref = Item::Item->new( "10056" );
So, you have the Item::Item object reference in $itemref. The statement,
@item = @$itemref; print $item[0]->{_description};

gave you the right thing because you treated it as data structure. But remember, your object is $itemref. So stick with it whenever you want to use it as object.

When I first learned about OOP (object oriented Perl :-), I found perlobj and perltoot as good introductions and enjoyful to read.


Udate: added print in relevant snippets

Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Replies are listed 'Best First'.
Re^2: bless array of hashes correct get method wrong
by peterb (Novice) on Apr 20, 2007 at 16:57 UTC

    Thanks to all, for all the help (I have been learning perl for a couple of months - all prior work procedural, and like it). I did not realise I would need to bless slices of an array to. I don't want to move the sql statement outside of the package so it calls the bless function six times (correct me if I am wrong) but have it all contained (except the where part of the query) in the package. So I followed your code, changing

    return bless \@objref, $class; to $self = [@objref]; return bless $self, $class; and changed the sub to sub get_desc() { my ($self, $idx) = @_ ; return $self->[$idx]{_description} ; }

    calling code from

    my $item = Item::Item->new( "10056" ); print $item->get_desc(0), "\n";

    it works!. However when I try the next entry (the sql statement returns 6 rows)

    my $item = Item::Item->new( "10056" ); print $item->get_desc(1), "\n";

    I get: Use of uninitialized value in print at testitem.pl line 17. In my while statement I assign each row to the $objref[$i++] slice. The $self = [@objref] should have the whole array reference (handle ? - not sure) and so why does it not work? More reading to do...

    .

      Sorry my mistake - this query only returns 1 row. I tried it with success on a multi-row query. Thank you.