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!


In reply to Re: bless array of hashes correct get method wrong by naikonta
in thread bless array of hashes correct get method wrong by peterb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.