Do you know where your variables are? | |
PerlMonks |
Conditional initialisationby Akhasha (Scribe) |
on May 20, 2004 at 02:17 UTC ( [id://354824]=perlquestion: print w/replies, xml ) | Need Help?? |
Akhasha has asked for the wisdom of the Perl Monks concerning the following question: Today I got something of a slap in the face. Well, at least it made me blush and there is a sting to thinking back on where I might have used a construct like this in a subroutine: my @list = @_ if @_ > 1;For the last few months I've been coding away and occasionally using this or something like it. I thought it would always initialise a new lexical and that the assignment was what the condition applied to. However it seems that in the above case for example, if @_ has 0 or 1 elements, then @list has the value it was assigned the last time the function was called and the condition held true. I just spent the last hour or so going back over my code and breaking this kind of construct into two lines:
Firstly I just want to caution my fellow monks to be on the lookout for it, as it works the first time and afterward can turn around to bite the naive. Secondly I'm wondering where this behaviour is clearly explained in the perl docs. I thought that because @list is a lexical, the value could not be there to be picked up on a second invokation of the function. Any pointers or observations on this would be appreciated. Thanks!
Back to
Seekers of Perl Wisdom
|
|