Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked


by Deanimal (Acolyte)
on Nov 05, 2002 at 17:56 UTC ( #210529=perlquestion: print w/replies, xml ) Need Help??
Deanimal has asked for the wisdom of the Perl Monks concerning the following question:

What does this ($|=1;) do? It's on the second line of a guestbook script, and I'm thinking it makes variables act differently than they would without it, because in another script for a survey (that doesn't contain this line) I'm trying to duplicate what happens in the guestbook script but it doesn't work. (Writing link on HTML page) The only difference I've found so far is this line. I searched for it in the documentation and CPAN and didn't find anything. Sorry if this is a stupid question.


Replies are listed 'Best First'.
Re: $|=1;
by Ovid (Cardinal) on Nov 05, 2002 at 18:16 UTC
(jeffa) Re: $|=1;
by jeffa (Bishop) on Nov 05, 2002 at 18:03 UTC
    $| is documented in perlvar:
    $|      If set to nonzero, forces a flush right away and
            after every write or print on the currently
            selected output channel.  Default is 0 (regardless
            of whether the channel is really buffered by the
            system or not; `$|' tells you only whether you've
            asked Perl explicitly to flush after each write).
            STDOUT will typically be line buffered if output
            is to the terminal and block buffered otherwise.
            Setting this variable is useful primarily when you
            are outputting to a pipe or socket, such as when
            you are running a Perl program under rsh and want
            to see the output as it's happening.  This has no
            effect on input buffering.  See the getc entry in
            the perlfunc manpage for that.  (Mnemonic: when
            you want your pipes to be piping hot.)


    (the triplet paradiddle with high-hat)
Re: $|=1;
by DigitalKitty (Parson) on Nov 05, 2002 at 18:03 UTC
    Hi Dean.

    Setting $| to 1 will turn off line buffering. Normally, a socket will wait until the buffer has filled before sending the data. This can be a source of trouble if the receiving socket is waiting and the data hasn't been sent yet. The $| = 1 simply eliminates the wait period before it sends.

    Hope this helps,
Re: $|=1;
by VSarkiss (Monsignor) on Nov 05, 2002 at 18:04 UTC

    It unbuffers the current output stream, so anything you write "appears as soon as possible". Look in perlvar. Also check out the I/O section in the Tutorials here.

Re: $|=1;
by rochlin (Acolyte) on Nov 05, 2002 at 21:19 UTC
    Another side note on the above answers: Why is $| used in the script you're looking at? It's a good idea to use it in any CGI script that is outputing a webpage. If the output buffered (no $|=1) then the delay can cause errors in the generated HTML or might just cause a timeout. It's always a good idea to prevent buffering in a CGI script that generates a webpage.

    For a nice place to live in Portland, OR

      $|=1; is currently in a script for a guestbook-- which obviously generates a webpage, so that makes sense. Thanks for explaining that. I'm having problems with another script-- for a survey (poll)-- that also generates a webpage. (My original question above has a link to it if you'd like to take a look) The problem is that a link in that webpage for "continue" doesn't contain the value that it's supposed to, and is instead just empty quotes (""). Maybe if I add this "unbuffering", then the link will print correctly. Sounds like it's worth a try. Thanks everybody!
Re: $|=1;
by Molt (Chaplain) on Nov 06, 2002 at 12:18 UTC

    If you're looking for, or at, guestbook scripts it may be worth you peering at nms which is a well-coded set of drop-in-replacements for the less-than-secure Matt's Script Archive scripts, as started and led by davorg.

    If nothing else the fact that they're commented may help you with things such as this. I would look long and hard at these before deciding I could do better myself though, they are very nice.

      Thanks, Molt. I'll look at those. Actually, what I'm using now are scripts from The guestbook that I have set up appears to be working fine. The problem I'm tring to solve is with a survey which won't print the url for "continue", as described in the other node which I link to in my first question above.
Re: $|=1;
by Anonymous Monk on Apr 23, 2003 at 17:42 UTC
    is this anything similar to the ReadKey (0) function???

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://210529]
Approved by John M. Dlugosz
and dust plays in a shaft of sunlight...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2018-03-25 02:05 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (299 votes). Check out past polls.