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

I wonder why print "\@retval contains $#retval \n"; returns 0, when $itm exists... Any ideas?
#============================= sub my_execer($) { my ($sql) = @_; my $sth = $dbh->prepare($sql) || undef; $sth->execute() || die "sth->execute: $DBI::errstr"; my @row; my @retval; if (@row = $sth->fetchrow_array) { my ($id, $prod) = @row; my $itm = new Item; $itm->id($id); $itm->type($prod); print Dumper $itm; push (@retval,\$itm); } $sth->finish; print "\@retval contains $#retval \n"; return (\@retval); }

Title edit by tye as single-word titles interfere with future simple searches

Replies are listed 'Best First'.
Re: push
by seattlejohn (Deacon) on Mar 14, 2002 at 20:11 UTC
    Because $#retval returns the maximum index in @retval, not the number of elements in it (use scalar @retval if that's what you want). If $#retval is 0, that means @retval contains a single value (i.e., $retval[0]).