peterb has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: bless array of hashes correct get method wrong
by Thelonius (Priest) on Apr 20, 2007 at 15:56 UTC | |
|
Re: bless array of hashes correct get method wrong
by naikonta (Curate) on Apr 20, 2007 at 16:02 UTC | |
by peterb (Novice) on Apr 20, 2007 at 16:57 UTC | |
by peterb (Novice) on Apr 20, 2007 at 18:32 UTC | |
|
Re: bless array of hashes correct get method wrong
by wfsp (Abbot) on Apr 20, 2007 at 15:57 UTC | |
|
Re: bless array of hashes correct get method wrong
by Herkum (Parson) on Apr 20, 2007 at 15:47 UTC | |
|
Re: bless array of hashes correct get method wrong
by logie17 (Friar) on Apr 20, 2007 at 15:39 UTC | |
|
Keep it simpler to start with
by doom (Deacon) on Apr 21, 2007 at 06:00 UTC |