in reply to 'my' buggy...
my ($cat) = @_ if @_;If @_ is empty, $cat will have the value it had the last time you were in the block. This is due to the way perl reuses scratchpads on block entry. Every sub (not every block, mind, but every sub) has a scratchpad associated with it, with space for all the my'd variables reserved in it. If, at runtime, you enter that sub and the default scratchpad isn't in use (because of recursive calls, for example) perl will use the default scratchpad. This saves on time, as the assumption is you won't be recursive and you will enter a sub more than once over the run of the program. Reusing the scratchpad means no time spent to allocate memory each time through.
The interesting bit is that initialization--giving the variables in the scratchpads values--is a runtime thing. Perl doesn't give a default to variables that have values assigned as part of the my, as that'd be a waste of time as well. (Why assign a default that's going to be immediately overwritten?) But your assignment is conditional, and in some cases it won't happen. That means that in those cases where the condition on the initial assignment isn't met no assignment happens at all. And, since perl's reusing scratchpads, you'll just get whatever was in there from the last time around.
Needless to say, this is often not what you want.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: 'my' buggy...
by japhy (Canon) on Apr 30, 2002 at 16:19 UTC | |
by Elian (Parson) on Apr 30, 2002 at 16:28 UTC | |
by ferrency (Deacon) on Apr 30, 2002 at 20:25 UTC | |
by Anonymous Monk on May 01, 2002 at 01:39 UTC | |
by Elian (Parson) on Apr 30, 2002 at 21:40 UTC | |
by mojotoad (Monsignor) on Apr 30, 2002 at 16:39 UTC | |
by Elian (Parson) on Apr 30, 2002 at 16:54 UTC | |
by japhy (Canon) on Apr 30, 2002 at 17:32 UTC |