> perl || echo perl died
warn "Pull my finger.\n";
die bless [], 'Fart';
warn "Excuse me.\n";
package Fart;
use overload '""' => 'pfft';
sub pfft { return '' }
__END__
Pull my finger.
perl died
| [reply] [Watch: Dir/Any] [d/l] |
To explain tye's whimsical (musical?) solution, a bit...
If you pass die a string exception that ends in anything other than a newline, Perl will helpfully insert its own message complete with file and line number. If you give it a "\n" like Eliya did, it won't add "Died at file, line", but you will still get the new line.
On the other hand, if you give die an object, Perl will try to stringify the object. Normally, that would result in something noisy like Fart=ARRAY(0x871ff0). However, if you use overload, you can provide your own rule for stringify-ing an object. In tye's example, he stringified it to an empty string, hence silence instead of farts.
My own much less humorous version, gets rid of the extra array creation and encapsulates the blessing and act of dying. It's not as funny, but maybe a bit more practical:
{
package DieSilently;
use overload q{""} => sub { '' };
sub now { die bless(\$_[0],$_[0]) }
}
print STDERR "Hello!\n";
DieSilently->now();
print STDERR "World!\n";
#outputs ($ are the command prompt)
$ Hello!
$
| [reply] [Watch: Dir/Any] [d/l] [select] |
| [reply] [Watch: Dir/Any] [d/l] [select] |
Thank you for the explanation, very much appreciated.
| [reply] [Watch: Dir/Any] |
The Exception::NoException CPAN module already does this. It's just a "false" object you can die with. die Exception::NoException->new
| [reply] [Watch: Dir/Any] [d/l] |
Not entirely silent, but maybe sufficient for the purpose: die "\n" ? (just adds a newline to what's already printed)
| [reply] [Watch: Dir/Any] [d/l] |
This is an awesome thread, especially the 'fart' code and the explanations of it. I recently ran into this "die quietly" issue using the below code. I was getting unwanted "\n"'s inserted that I could not explain. I ended up removing the "die" statement and it works as I want, but I am not sure if I am doing something incorrect (not killing my system command?). This is really my first attempt in using a timed out system command via eval/alarm. Here is my code if you care to review it and give me some feedback.
sub get_size {
my $disk=$_[0];
my $size;
eval {
local $SIG{ALRM} = sub {
print_debug("get_size timed out for device $disk");
die;
};
alarm(4);
$size=qx(bootinfo -s $disk 2>/dev/null);
alarm(0);
};
$size=($size)? $size : "unknown";
chomp $size;
return $size;
}
| [reply] [Watch: Dir/Any] [d/l] |