Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Puzzling $| behavior

by dirving (Friar)
on Oct 07, 2007 at 21:45 UTC ( #643330=note: print w/ replies, xml ) Need Help??


in reply to Puzzling $| behavior

Perl's behavior isn't defined when you read the value of a variable and then auto-increment/decrement it later in the same statement. Try doing this instead and you'll get the behavior you expect:

$| = 0; print "first=$|,"; $|--; print "second=$|\n"; $| = 1; print "first=$|,"; $|--; print "second=$|\n";

In your first case, Perl evaluates the second $|, then decrements it, then evaluates the first one, leading to the unexpected behavior. The reason you don't end up with "-1" is that there is magic associated with $|. It doesn't actually hold a value, it just tracks whether it is set to a non-zero value or not. Try for instance  $| = -3; print $| to see this behavior.

Read perlvar and perlop for more information on what is going on here.

-- David Irving


Comment on Re: Puzzling $| behavior
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (11)
As of 2014-08-29 14:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (280 votes), past polls