On 8/17/07, Chas Owens <[EMAIL PROTECTED]> wrote:
snip
> Do you mean "broken" as in it doesn't work, it shouldn't work, or it
> is a bad idea?  While I think it shouldn't work and it is a bad idea,
> it does, much to my surprise when I tested it before posting*,
> demonstrably** work with Perl 5.8.8 and Perl 5.005 (after removing the
> warnings pragma)
snip

And so much for the shouldn't work.  On page 223 of Programming Perl
3rd edition it even demonstrates it:

Although Perl doesn't include the word "static" in its lexicon, Perl
Programmers have no problem creating variables that are private to a
function and persist across function calls.  There is no special word
for these.  Perl's richer scoping primitives combine with automatic
memory management in ways that someone looking for a "static" keyword
might never think of trying.

Lexical variables don't automatically get garbage collected just
because their scope has exited; they wait to get recycled until
they're no longer used, which is much more important.  To create
private variables that aren't automatically reset across function
calls, enclose the whole function in an extra block and put both the
my declaration and the function definition within that block.  You can
even put more than one function there for shared access to an
otherwise private variable:

    {
        my $counter = 0;
        sub next_counter { return ++$counter }
        sub prev_counter { return --$counter }
    }

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to