Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Static variables (oh, the horror())

by ChemBoy (Priest)
on May 21, 2003 at 22:07 UTC ( #259912=note: print w/ replies, xml ) Need Help??

in reply to Re: Re: Re: Re: Never lock $0 inside of a BEGIN block
in thread Never lock $0 inside of a BEGIN block

BTW, the static trick only works with if 0; and for ();

Not so. Observe:

#!/usr/local/bin/perl -l sub horror { my $foo if "reset" eq shift; print ++$foo; } horror($_) for qw(foo bar baz reset bing bang boom); __END__ 1 2 3 4 1 2 3

(Not that the sub name is intended as an editorial comment, or anything... <grin>)

The nasty part is just that the cleanup (resetting $foo to undef) takes place at the end of the iteration in which my $foo is actually executed (which isn't exactly what you'd expect from a "reset" argument, but that's what you get with silly example code).

If God had meant us to fly, he would *never* have given us the railroads.
    --Michael Flanders

Comment on Static variables (oh, the horror())
Download Code
Replies are listed 'Best First'.
Re: Static variables (oh, the horror())
by demerphq (Chancellor) on May 22, 2003 at 08:39 UTC

    Not so.

    Yes so. The code you demonstrate illustrates this nicely too. Perhaps you thought I meant a literal 0? I didnt I meant if FALSE. This behaviour while nice is a bug IMO.


    <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://259912]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2015-10-06 23:09 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (163 votes), past polls