http://qs321.pair.com?node_id=889457


in reply to Re: Die silently? (/^but+$/)
in thread Die silently?

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