Re: The print is output later after die
by choroba (Cardinal) on Jan 28, 2022 at 09:48 UTC
|
#!/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]
| [reply] [d/l] [select] |
|
| [reply] |
|
*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
| [reply] [d/l] [select] |
|
$| = 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.
| [reply] [d/l] [select] |
|
| [reply] [d/l] [select] |
|
See IO::Handle. There's no difference, just readability :-)
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
| [reply] [d/l] |
|
|
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
| [reply] [d/l] [select] |
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.
| [reply] [d/l] [select] |
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" :)
| [reply] |
|
| [reply] [d/l] |
|
Yeah I was up to posting it already, but hippo linked to the FAQ which in turn links to it.
| [reply] |