Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Poor Perl Idioms Explained (except not really)

by diotalevi (Canon)
on Aug 01, 2003 at 16:40 UTC ( #280038=note: print w/ replies, xml ) Need Help??


in reply to Perl Idioms Explained - $|++

This is a pet peeve of mine. I really, really dislike it when people use decrement or increment on $|. If $| is already zero then decrementing it doesn't cause it to remain false, it becomes true. Ugh! I think that by using increment on $| you imply that decrement would also be sane. It isn't and the only sane way to cause $| to be false is through an assignment. Arithmetic on $| is hooey.

Added note: you're supposed to say $| = 1 and $| = 0. At least then you know what the value of $| will be.

$ perl -le 'for(0..4){print $|--}' 0 1 0 1 0 $ perl -le 'for(0..4){print $|++}' 0 1 1 1 1


Comment on Re: Poor Perl Idioms Explained (except not really)
Download Code
Re: Re: Poor Perl Idioms Explained (except not really)
by Elian (Parson) on Aug 01, 2003 at 18:02 UTC
    I completely agree. $|++ gives the erroneous impression that you can somehow have nested $| setting, and I've seen people bitten by this when they think that they're still set to autoflush (because of nested increments) when they aren't. It's a bad idiom, and I'm half-tempted to make $| roll over and toggle state in perl 6. (Which I know won't happen, which is fine, the same way that perl 6 won't deliver 20kV across the keyboard of anyone who writes code like $foo ? bax($bar) ? 12 : xyzzy(34) : $x or $y ? foo() : bar() though I can but dream)
(jeffa) Re: Poor Perl Idioms Explained (except not really)
by jeffa (Chancellor) on Aug 02, 2003 at 14:12 UTC
    An old manager of mine saw $|++ in some of my code and requested i change it to $| = 1. I asked why and he replied that it's better to explicitly set something than to assume it is, say, zero. I know that $| defaults to 0, you know that $| defaults to 0 ... but what if someday the default for $| is instead 1?

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      what if someday the default for $| is instead 1?
      That's not the problem:
      $ perl -le '$| = 1; $|++; print $|++' 1
      The problem is that it might confuse someone who doesn't know about this particular piece of DWIMmery.

      Update: removed unfunny joke

        Even more obvious:
        $ perl -le '$| = 42; print $|;$|--;print $|' 1 0
        So $|++ always sets it to true, and $|-- toggles it. Useful to know, but not obvious, and not consistent. But then DWIMmery is not necessarily consistent.

        -QM
        --
        Quantum Mechanics: The dreams stuff is made of

        I can tell you didn't actually test this.

        Actually, i did test it. But you try telling contrary to a manager that won't listen to you. ;) Even if you show him such code. And no ... i don't work for him anymore. :)

        Anyways, the point was not what it is now, but what will it be if it changed sometime in the future.

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2014-10-02 08:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (52 votes), past polls