Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Perl Idioms Explained - $|++

by NetWallah (Abbot)
on Aug 01, 2003 at 19:21 UTC ( #280112=note: print w/ replies, xml ) Need Help??


in reply to Perl Idioms Explained - $|++

Absolutely agree with the peeve.

I would like to see this coded like this:

use strict; use warnings; use constant {OUTPUT_AUTOFLUSH_BUFFERED =>0, OUTPUT_AUTOFLUSH_UNBUFFERED=>1}; $| = OUTPUT_AUTOFLUSH_UNBUFFERED; ...
This makes the meaning MUCH clearer.


Comment on Re: Perl Idioms Explained - $|++
Download Code
Re: Re: Perl Idioms Explained - $|++
by ctilmes (Priest) on Aug 01, 2003 at 21:09 UTC
    Well if you're going that far, you might as well use English; and use $OUTPUT_AUTOFLUSH too.
Re: Re: Perl Idioms Explained - $|++
by sauoq (Abbot) on Aug 01, 2003 at 21:26 UTC
    I would like to see this coded like this:

    Ugh. I wouldn't. First, we don't need Yet-Another-Pair-Of-Constants-For-Zero-And-One. Really, most of us are pretty comfortable with the fact that 0 means 'false', 'no', or 'off' and that 1 means 'true', 'yes', or 'on' depending on context. Defining more aliases for every place where a zero or one will do is just false hubris.

    Constants are good for two things. They are good when they can be used to replace a long or difficult-to-remember literal value. Something like use constant PI => 3.14159265358979; would fit in this category of use. The other time to use constants, as unintuitive as it seems, is when they might need to change. That is, when they might need to change between platforms, installations, or even executions but they need to remain constant throughout any single execution. Constants named things like MAXINT, INSTALL_BASE, and DEBUG probably fall in this category. Of course, the two categories aren't mutually exclusive. The LOCK_* constants provided by Fcntl might be an example of some that are good for both reasons.

    This makes the meaning MUCH clearer.

    Again, I disagree. It doesn't make the meaning clearer at all. The uninitiated user will still wonder what that $| variable is. That is the point that needs to be clarified not that a 1 means on and a 0 means off. Of course, you can make it clear with the line use English; which will provide you with the nicely named $OUTPUT_AUTOFLUSH alias.

    Just the same, I'm completely comfortable with idiomatic perl and I don't use the English module. (If someone maintaining my code isn't already familiar with most special variables and doesn't at least know how to look them up in perlvar, then they're probably in over their heads anyway.) I use $| = 1; with abandon in smaller scripts¹.

    Besides, even a nice alias for the admittedly esoteric $| doesn't address the larger problem that no one has yet mentioned. That is, $| is associated with the currently selected filehandle. Keeping track of that can be difficult. In larger scripts, I simultaneously handle that and the clarity issue by writing code like this

    use IO::Handle; STDOUT->autoflush;

    1. And yes, I'm guilty of using $|++ as well despite the fact that I agree it has drawbacks.

    -sauoq
    "My two cents aren't worth a dime.";
    

      In larger scripts, I simultaneously handle that and the clarity issue by writing code like this
      001 use IO::Handle; 002 STDOUT->autoflush;

      I got kinda turned off of IO->Handle when I read this comment in the pod

      use IO::Handle; # thousands of lines just for autoflush :-(

      When I read the authors name, it strengthened the feeling.

      Update: To clarify, the author to which I referred was the author of the comment I quoted above--from the pod of perlipc not IO->Handle--Tom Christiansen, who comments are to be valued.

      I have absolutely nothing against Graham Barr (the author of IO::Handle), nor even much against that module.

      My only reservation was of the practice of using IO->Handle only for the purposes of turning on autoflush.

      If $|=1; $|=0; is deemed to cryptic, you could use something like:

      sub autoflush (*$) { # Set the state of autoflush for # the handle supplied as $_[0] # to the boolean state supplied as $_[1] select( do{ my $old = select( $_[0] ); $| = $_[1]; $old; } ) } ... autoflush( STDOUT, 1 ); # autoflush on ... autoflush( STDOUT, 0 ); # autoflush off

      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
      If I understand your problem, I can solve it! Of course, the same can be said for you.

        My only reservation was of the practice of using IO->Handle only for the purposes of turning on autoflush.

        I agree. I said "longer scripts" and most of my longer scripts already pull use IO::Handle somewhere along the line anyway. I probably should have said that it's not a good idea to use it just for autoflush.

        -sauoq
        "My two cents aren't worth a dime.";
        
      First, we don't need Yet-Another-Pair-Of-Constants-For-Zero-And-One.

      I've come across cases where it helps readability. Specifically, when there are many cases where some arguments to the same function are actually interpreted as numbers and some others are booleans, then it can be much more maintainable to see something like $button->set_border(TRUE, 0, 2.5);, which clarifies that the 0 was meant as an actual numerical value of zero.

      I've never had a case where I wanted any more named constants than for TRUE and FALSE, though.

      Makeshifts last the longest.

Re: Re: Perl Idioms Explained - $|++
by Anonymous Monk on Apr 14, 2004 at 17:46 UTC
    How about using a comment instead?

    $|=1; # autoflush

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2014-12-28 16:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (182 votes), past polls