On Feb 4, Nilay   Puri, Noida said:

>> Can any one help me understand the usage of special variable $| ?
>>
>> I know the description of this var. If set to nonzero, forces a flush
>> after every write or print.

Most filehandles buffer their output until a newline is reached.  For
example, try this code:

  for (1 .. 5) {
    print STDOUT;
    sleep 1;
  }

At the end of 5 seconds, you'll get "12345".  Compare that with this:

  for (1 .. 5) {
    print STDERR;
    sleep 1;
  }

Here, you get 1, then a pause, 2, then a pause, and so on.  STDOUT is
buffered, whereas STDERR is not.  Now, if you want to UNbuffer STDOUT, you
set $| to 1.

  $| = 1;
  for (1 .. 5) {
    print STDOUT;
    sleep 1;
  }

Now you get 1, pause, 2, pause, 3, pause, 4, pause, 5, pause.

$| has its own value for each filehandle.  In order to make filehandle FOO
unbuffered, you need to select() FOO and then set $| to 1.

  my $old_fh = select FOO;
  $| = 1;  # now FOO is unbuffered
  select $old_fh;  # go back to whatever filehandle was previous selected

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to