Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Die silently?

by Anonymous Monk
on Feb 21, 2011 at 17:09 UTC ( #889425=perlquestion: print w/replies, xml ) Need Help??

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

Is there a way to die silently (e.g. die "" or at least die " " or die "\0") without Perl setting $@ to "Died")? My problem: another routine has printed error messages to STDERR and I just need to add the die() part.

Note: prefer to use die() instead of exit() since the outer code optionally catches with eval {} (but usually it doesn't and just let the die message be printed to STDERR). Prefer not having to use Capture::Tiny or other non-core modules.

Replies are listed 'Best First'.
Re: Die silently? (/^but+$/)
by tye (Sage) on Feb 21, 2011 at 18:10 UTC
    > 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

    - tye        

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

        You're mildly incorrect. Perl does not automatically stringify your exception objects. It leaves them alone and they're just like a regular objects. tye's object happens to have an overloaded conversion but that's only invoked when you examine it by printing $@ or testing to see if $@ is true/false.

        ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

        Thank you for the explanation, very much appreciated.
Re: Die silently?
by diotalevi (Canon) on Feb 21, 2011 at 18:41 UTC

    The Exception::NoException CPAN module already does this. It's just a "false" object you can die with.

    die Exception::NoException->new
Re: Die silently?
by Eliya (Vicar) on Feb 21, 2011 at 17:23 UTC

    Not entirely silent, but maybe sufficient for the purpose: die "\n" ?   (just adds a newline to what's already printed)

Re: Die silently?
by gg48gg (Sexton) on Feb 20, 2013 at 20:10 UTC

    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; }

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2020-10-28 09:41 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (260 votes). Check out past polls.