Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

my $foo if 0; is a bug

by tilly (Archbishop)
on Jul 15, 2001 at 04:58 UTC ( #96802=note: print w/ replies, xml ) Need Help??


in reply to It *is* a feature: mod_perl and 'my $x if 0'

You are seeing the bug in a form where it almost makes sense. However it really is a bug. When I first saw this, it was a bug in Number::Format which turned out to be due to a construct of this form:

my $foo = $some_init unless test();
The resulting bug is that if the test failed on 2 calls in a row, you had data from the first call of the function that polluted your output results. This bug was quite subtle and mysterious, and it turned out had been reported to the module author several times over the course of a year, but he was unable to figure out where the bug was.

Since then whenever I have looked at a large body of code for either of the patterns:

/my .* if/ /my .* unless/
and found a match, it has usually resulted in also finding a bug. In short, this is a "feature" that is due to a broken optimization of Perl which, when it is hit in code, is almost always the cause of a bug in that code.

Now if you want static lexical variables without the usual "create a private scope" trick, that is doable in a fairly straightforward manner. In fact I just wrote and tested an implementation, which I just uploaded to CPAN. Hopefully Tie::Static will be accepted and become available shortly. (I intend to post the module to PerlMonks in a short while as well.)


Comment on my $foo if 0; is a bug
Select or Download Code
Re: my $foo if 0; is a bug
by MeowChow (Vicar) on Jul 15, 2001 at 07:42 UTC
    You make a very good point, and one that I whole-heartedly agree with. As I said, I wouldn't use this sort of thing in real code, but I thought it's worth demonstrating that there are cases where it's quite useful to be able to declare a variable of static lifetime without having to create an enclosing scope. Yes, I see that it can be done with tie, though I usually avoid tied variables after having had a few nasty experiences with their performance. The requisite caller voodoo makes a tied implementation even less appealing.
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2014-09-18 21:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (124 votes), past polls