|Do you know where your variables are?|
Re^3: "defined" in while loop Edit | Delete | Quote | Reply | Private Replyby ELISHEVA (Prior)
|on Feb 17, 2009 at 00:34 UTC ( #744249=note: print w/replies, xml )||Need Help??|
Thanks! Learn something every day! (well, lots today). But what is going on here? What (under the covers) makes this a special case? Why is Perl wrapping $_=<> in defined($_ =<>)? Another convenience feature? And where else does this happen?
Here's my output with a slightly expanded print line checking both the value and the defined status and just to make sure there was no monkey business with the special variable $_ I changed it to $line - same result as you, of course. (I had to switch " and ' to keep the bash shell from trying to resolve $_):
It is also not anything special about the syntax $somevar = <> because I get the same result if I use readline(...):
Or is it? Could it be that both while($somevar = readline(...)) and while($somevar = <...>), but only these, have special semantics? I noticed that if I wrap readline in a function of my own that the special behavior goes away. For example,
Thanks in advance, beth
Update: refined question a bit; fixed error in sub of $_ with $line; added examples using readline and a sub wrapping readline.
In Section Seekers of Perl Wisdom