Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: The weirdest thing....

by Joost (Canon)
on May 09, 2007 at 21:32 UTC ( #614500=note: print w/replies, xml ) Need Help??


in reply to Re: The weirdest thing....
in thread The weirdest thing....

I'm nitpicking here, but note that $|++ does not increment $| when it's 1. $| is always 1 (flush) or 0 (buffer).

That means that:

# INCORRECT sub something { $|++; # do something without buffering $|--; }
Will turn on buffering even if it was off before calling the subroutine.

Usually that's not really a problem, but if you need to revert autoflush to its previous setting, you have to remember its status yourself:

#CORRECT sub something { my $flush = $|++; # do something without buffering $| = $flush; }

Replies are listed 'Best First'.
Re^3: The weirdest thing....
by jdporter (Chancellor) on May 09, 2007 at 21:36 UTC
    #CORRECT sub something { local $| = 1; # do something with autoflushing }
Re^3: The weirdest thing....
by former33t (Scribe) on May 10, 2007 at 19:25 UTC
    Yes, of course you are correct. It's probably even a smidgen faster to just assign a 1 to $| than it is to perform the computation involved in incrementing. I don't think I've ever run into a situation where I wanted autoflush on a filehandle sometimes and then go back to buffering, but I'll keep that in mind.

    I do like the use of setting the autoflush locally in your other post.

Re^3: The weirdest thing....
by ikegami (Patriarch) on May 12, 2007 at 16:15 UTC

    I'm nitpicking here, but note that $|++ does not increment $| when it's 1. $| is always 1 (flush) or 0 (buffer).

    You can use =1 instead of ++ and you wouldn't have to explain this. Be nice to your reader and use =1.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2022-10-01 15:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (1 votes). Check out past polls.

    Notices?