Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://643330]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2018-06-24 17:32 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.