Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: simple regex question

by JavaFan (Canon)
on Nov 26, 2010 at 15:08 UTC ( [id://873877]=note: print w/replies, xml ) Need Help??


in reply to Re: simple regex question
in thread simple regex question

$|++
Why?

Is there a reason to insert obscure, needless, lines in your program? Considering that all you're are doing is writing newline terminated strings to STDOUT, which already flushes on newlines by default, there isn't much of a reason to disable buffering.

But if, for some reason, you want to disable buffering, why this syntax? What's wrong with a plain $| = 1;? You do realize that not everyone knows what the result of $|++;$|++;$|--;$|--; is?

Replies are listed 'Best First'.
Re^3: simple regex question
by phenom (Chaplain) on Nov 26, 2010 at 15:16 UTC
    You're absolutely correct. It's in there because it's part of a template I use for everything I write. Just habit, I suppose.

    I also agree that $| = 1 is more legible. But people who know about removing obscure, needless lines from programs will also know what it means. This too is just another habit - which even perlcritic doesn't complain about.

Re^3: simple regex question
by Marshall (Canon) on Nov 27, 2010 at 04:56 UTC
    There are some caveats with buffering on STDOUT. The behavior changes if Perl figures that STDOUT has been redirected to something not a physical console. Consider:
    #!/usr/bin/perl -w use strict; for (0..200) { print "$_ buffered xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n"; print STDERR "$_ unbuffered \n"; }
    When run from the command line, STDOUT is indeed unbuffered and you see alternating buffered/unbuffered statements. However for example if I run this in Textpad (my editor on Windows - I think similar to Notepad++) and have it capture the output, STDOUT is buffered. And the first buffered line is output after about 88 lines. I conclude that on my Windows system the buffer is about 8KB. So it depends upon where STDOUT is directed to.

      I'm fully aware of that, and that's why I wrote by default. It's completely irrelevant for the program I was replying to, as it wasn't writing to different filehandles, and then outside of the program merging them again. It was writing to STDOUT, and nothing else. If it was being redirected to a file, you don't need to unbuffer.

      But my beef wasn't so much about the unbuffering. It was about the idiom that should never have been cargo culted into widespread use.

      Yes, and I know who to blame.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-03-28 15:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found