Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

The print is output later after die

by Anonymous Monk
on Jan 28, 2022 at 09:39 UTC ( [id://11140913]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How come Perl's print is output later after die output when the print code line really precedes the die one ?
How to solve to be correct intuitively

Replies are listed 'Best First'.
Re: The print is output later after die
by choroba (Cardinal) on Jan 28, 2022 at 09:48 UTC
    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]

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


      🦛

      *STDOUT->autoflush;

      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 ?

      Cheers,
      Rob

        $| = 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

        L*

        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]

      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

Re: The print is output later after die
by Discipulus (Canon) on Jan 28, 2022 at 09:47 UTC
    Hello Anonymous Monk,

    your question is really poorly expressed. By what I'm able to decipher I suggest you to investigate the fact perl has always 3 opened filhandle and letting apart STDIN you can experiencing something bound to different behaviour of STDOUT (used by default by print) and STDERR (used by die).

    Also buffering can be involved, also some poor minded OS (aka windows) make a mess of STDERR in their console.

    Some line of code will be more appreciated.

    L*

    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.
Re: The print is output later after die
by RMGir (Prior) on Jan 28, 2022 at 20:04 UTC
    All the other answers are excellent - all that's missing is pointing out the usual (and humourous) name for this problem is "suffering from buffering" :)

    Mike

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11140913]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-19 20:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found