perl 5.8.8
(use strict; -wT, use diagnostics)

I have traveled googles of miles to reach your doorstep... I am new to OOP (Damian Conway's book is helping). I use the array of hashes as the query resultset returns multiple rows. Thinking: I want the benefits of arrays so put the hashes in the array rather than only hashes. I bless an array of hashes (the result of a sql query) and test it using

my $itemref = Item::Item->new( "10056" ); my @item = @$itemref; print $item[0]->{_description}, "\n";

works fine. I have a method in the Item::Item file

sub get_desc { $_[0]->{_description}; }

when I try
print $item[0]->get_desc(), "\n";
I get:

Can't call method "get_desc" on unblessed reference at testitem.pl lin +e 17 (#1) (F) A method call must know in what package it's supposed to run. + It ordinarily finds this out from the object reference you supply, b +ut you didn't supply an object reference in this case. A reference i +sn't an object reference until it has been blessed. See perlobj. Uncaught exception from user code: Can't call method "get_desc" on unblessed reference at testite +m.pl line 17. at testitem.pl line 17

My reading suggests a) I need to bless the sub or b) closure. Neither of which I want to do or am familiar with. I know I want to use get_ and set_ methods and believe this is a simple syntax problem on either the sub or my calling of the sub. I have tried in the sub get_desc

sub get_desc { my (@class, $count) = @_; return $class[$count]->{_description}; } At the print statement print @item->get_desc(), "\n"; print $itemref->get_desc(), "\n";

all with no success. Frankly I am guessing... Thank you,

Under sub new I do

sub new { my ($class, @arg) = @_; $class->_incr_count(); my (@item, $ref, @objref); $sth->execute($company, $_[1]); $sth->bind_columns(undef, \$item[0], \$item[1], etc.. while ($ref = $sth->fetchrow_arrayref()) { $objref[$i] = ( { _item => $item[0] || "", _available => $item[1] || "", _brand => $item[2] || "", etc.. } ); $i++; } return bless \@objref, $class; } # End sub new

based on Herkum I have now tried

sub get_desc() { my $self = shift; if (ref $self) { return ${ $self->{"_description"} }; } else { return $self->{_description}; } }

and called it this way

print $itemref->get_desc(), "\n";

gives

Pseudo-hashes are deprecated at /usr/local/www/cgi-pbin/pmpl/item.pl l +ine 119 (#1) (D deprecated) Pseudo-hashes were deprecated in Perl 5.8.0 and th +ey will be removed in Perl 5.10.0, see perl58delta for more details. +You can continue to use the fields pragma. Argument "POLLOCK SUBMARINER BTR 3.2 OZS" isn't numeric in hash elemen +t at /usr/local/www/cgi-pbin/pmpl/item.pl line 119 (#2) (W numeric) The indicated string was fed as an argument to an operator + that expected a numeric value instead. If you're fortunate the mess +age will identify which operator was so unfortunate. Bad index while coercing array into hash at /usr/local/www/cgi-pbin/pm +pl/item.pl line 119 (#3)(F) The index looked up in the hash found as +the 0'th element of a pseudo-hash is not legal. Index values must be + at 1 or greater. See perlref. Uncaught exception from user code: Bad index while coercing array into hash at /usr/local/www/cgi +-pbin/pmpl/item.pl line 119. at /usr/local/www/cgi-pbin/pmpl/item.pl line 119 Item::Item::get_desc('Item::Item=ARRAY(0x8135c88)') called at +testitem.pl line 17

Thank you


In reply to 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.