Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: Boolean counter?

by shawnhcorey (Friar)
on Dec 07, 2009 at 14:07 UTC ( #811504=note: print w/ replies, xml ) Need Help??


in reply to Re: Boolean counter?
in thread Boolean counter?

$Counter = - --$Counter;


Comment on Re^2: Boolean counter?
Download Code
Replies are listed 'Best First'.
Re^3: Boolean counter?
by ikegami (Pope) on Dec 07, 2009 at 17:08 UTC

    Don't modify a variable that you're using elsewhere in an expression. At best, it's unclear. At worse, the result is undefined.

    $toggle = - --$toggle;
    should be
    $toggle = -( $toggle - 1 );
    which can be shortened to previously mentioned
    $toggle = 1 - $toggle;
Re^3: Boolean counter?
by JavaFan (Canon) on Dec 07, 2009 at 15:13 UTC
    The behaviour of that is actually not defined. You may end up with $Counter being -1, or with purple daemons coming out of your USB port, ready to chew off your fingers.
      $i appears only once, the behaviour is defined
      $ perl -MO=Deparse,-p -le"print - --$i" BEGIN { $/ = "\n"; $\ = "\n"; } print((-(--$i))); -e syntax OK
      You were thinking of
      print(-( ++$i - --$i ))

        While the operand evaluation order for the ops you've used in your example isn't defined, it doesn't matter (unless there's an overflow) for that example. It's just a clarity issue.

        $i appears only once, the behaviour is defined

        Except it ($Counter) appeared twice.

      What about it is not defined?

        duh, you're referring to the fact that he's assigning to the variable he just modified. I was still asleep.

        While such constructs should be avoided over clarity concerns, it's wrong to say it's undefined. The precedence and evaluation order for that statement is actually clearly defined in perlop.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2015-07-30 04:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (269 votes), past polls