Gossamer Forum
Home : General : Perl Programming :

Confused

Quote Reply
Confused
This has me confused...

Code:
while (my $rec = $sth->fetchrow_hashred) {

}

This code goes into an eternal loop. Putting:

die %$rec;

..inside the loop dies with:

"Can't use undefined value as a hashref"

Eh?...surely if it is undefined the while loop should end?
Quote Reply
Re: [Paul] Confused In reply to
Is that a typo in your code? hashred.

- wil
Quote Reply
Re: [Wil] Confused In reply to
Yeah. I probably would have got a fatal error if it was in the code :)
Quote Reply
Re: [Paul] Confused In reply to
what does die $rec show?

did you solve this problem yet?
Quote Reply
Re: [adrockjames] Confused In reply to
I mentioned what it showed at the bottom of post 1 :)

I was slightly inaccurate in post 1, I actually had:

"my ($rec)" and not "my $rec"

Changing to "my $rec" solved the problem, however I still don't really understand the problem because $sth->fetchrow_hashref should be returning an empty hashref if there are no rows and so $rec should equal {} ....so it seems with the parenthesis it is determined to be defined and without () then it is undefined.

I'm a little lost by that.

Last edited by:

Paul: Jun 26, 2003, 11:59 AM
Quote Reply
Re: [Paul] Confused In reply to
> I mentioned what it showed at the bottom of post 1 :)

oh yeah, sorry

fetchrow_hashref returns undef if either an error occurs or if there are no rows

(my ($rec) = undef) returns 1.
Quote Reply
Re: [adrockjames] Confused In reply to
Quote:
(my ($rec) = undef) returns 1.

Hmm not in the test I just did.

Code:
perl -e "print (my ($rec) = undef);"

Last edited by:

Paul: Jun 26, 2003, 2:35 PM
Quote Reply
Re: [Paul] Confused In reply to
odd, because

$ perl -e "print \"return = \" . (my ($rec) = undef);"

gives

return = 1
Quote Reply
Re: [adrockjames] Confused In reply to
Weird, the following returns undef:

Code:
perl -e "print (my ($rec) = undef);"

...but the following returns 11....

Code:
perl -e "print 1 . (my ($rec) = undef);"

Last edited by:

Paul: Jun 26, 2003, 2:52 PM
Quote Reply
Re: [Paul] Confused In reply to
It's because you are creating a list with one element in it that is undef which is true. Compare:

[alex@penguin alex]$ perl -le 'print ((my ($f) = undef) ? "true" : "false")'
true
[alex@penguin alex]$ perl -le 'print ((my $f = undef) ? "true" : "false")'
false
[alex@penguin alex]$

The first one ends up evaluating a list with one element: (undef), which is true.

Cheers,

Alex
--
Gossamer Threads Inc.