I still miss the reason of WHY although i'am redirecting all error to the new standard output which after the above code is the computer screen, i still i get both my print statememnt printed and another error as well. Why does die print 2 errors in this case?
I think you're still missing what die really does. To quote from the docs: "Outside an "eval", prints the value of LIST to "STDERR" and
exits with the current value of $! (errno)." Note that the emphasis is mine: then the document goes at some length into explaining that things are not really that simple. But the point is that die exits with a value that indicates an error, independently of what gets printed to where. These are orthogonal consequences. See the following example script:
#!/usr/bin/perl
use strict;
use warnings;
if (@ARGV) {
my $msg="This is printed to STDOUT\n";
{
_die => sub () {
local *STDERR=*STDOUT;
die $msg;
},
_print => sub () {
print $msg;
exit 0;
}
}->{+shift}->();
}
sub chk {
my $cmd=shift;
my $ret=system $0 => $cmd;
warn "Failure to run <$cmd>: $!\n"
and return if $ret == -1;
warn "<$cmd> died with status: ", $? >> 8, "\n"
unless $ret == 0;
}
chk $_ for qw/_print _die/;
__END__
It gives me:
This is printed to STDOUT
This is printed to STDOUT
<_die> died with status: 255
|