Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: The print is output later after die

by choroba (Archbishop)
on Jan 28, 2022 at 09:48 UTC ( #11140916=note: print w/replies, xml ) Need Help??

in reply to The print is output later after die

STDOUT which print uses by default is buffered. die outputs to STDERR which is not buffered, therefore it shows immediately.

The buffering can be turned off:

#!/usr/bin/perl use warnings; use strict; # Uncomment to change the order: # *STDOUT->autoflush; print "abc"; die "ABC";

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: The print is output later after die
by hippo (Bishop) on Jan 28, 2022 at 10:52 UTC

    Good example (++). And for the benefit of our anonymous wisdom seeker there's always the relevant FAQ.


Re^2: The print is output later after die
by syphilis (Archbishop) on Jan 28, 2022 at 11:51 UTC

    My goto answer would have been $|++.
    I'm thinking it's a case of "six of one; half-a-dozen of the other".
    Is there a difference ?


      $| = 1; affects the currently selected handle. This is the handle print, printf and say use if you don't provide one., and it defaults to STDOUT.

      In fact, ->autoflush changes the currently selected handle to the invocant, sets $|, then restores the originally selected handle.

      Hello syphilis,

      differences? perlvar says that $|++ flush right away and after every write or print on the currently selected output channel while *STDOUT->autoflush only affect STDOUT your example instead any select -ed FH and so also STDERR in case of warn or die


      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      See IO::Handle. There's no difference, just readability :-)

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

        There's a difference. $| = 1; is equivalent to select()->autoflush(), not STDOUT->autoflush().

Re^2: The print is output later after die
by eyepopslikeamosquito (Bishop) on Jan 29, 2022 at 02:21 UTC

    Nice example! I prefer autoflush because the intent is much clearer than messing with the $| global variable.

    As an aside, it's a shame Perl was originally released with so many evil global variables because by the time lexical file handles made it into Perl 5.6 a host of unfortunate idioms were in common use, such as:

    select((select(FH), $|=1)[0]);

    Of course, such old coding horrors like this can be expressed more clearly nowadays with a lexical file handle (see also):

    use IO::Handle; # update: not needed for Perl 5.14+ # ... $fh->autoflush();

    For more examples of historic interface design boo-boos, see the Some Examples of Interface Mistakes section at: On Interfaces and APIs

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2022-09-25 20:18 GMT
Find Nodes?
    Voting Booth?
    I prefer my indexes to start at:

    Results (116 votes). Check out past polls.