Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Any last words?

by broquaint (Abbot)
on Nov 22, 2001 at 16:50 UTC ( #126960=perlmeditation: print w/replies, xml ) Need Help??

Playing about with signal handlers the other day, I came across a rather unusual caveat - when you pass multiple arguments to die() or warn() they are concatenated together, whether they are strings or not, and put into the first parameter of the handlers.
use strict; $SIG{'__DIE__'} = sub { local $" = ', '; # this will have no effect on @errs my @errs = @_; print "$0: @errs"; exit(1); }; die('foo', {'bar' => 'xxx'}, ['baz','quux']); __END__ output - fooHASH(0x80f9964)ARRAY(0x810a014) at line 9.
This is counter-intuitive since one expects to pass a LIST and get the same at the other end. While I am aware that signal handlers traditionally only take a single argument, this particular caveat is not explained in the docs (although it alludes to taking a single argument, as the example code always used $_[0] when referring to the arguments). Also, I'm fairly sure this is consistent across all platforms since the concatenation is hard-coded in the perl source (i.e no platform dependent #ifdef's).

So this is not so much a bug (as I first thought it might be) as an inconsistency between the documentation and the code. Perhaps some wisened monks out there could enlighten me as to the purpose of this 'feature' ...


Replies are listed 'Best First'.
Re: Any last words?
by Anonymous Monk on Nov 23, 2001 at 02:22 UTC
    No man, this is plain parameter passing rules in action. You are passing references.

    This will do:

    my %hash= qq(bar xxx);

    my @array = qq(baz quux);

    die('foo ',%hash, @array);


Re: Any last words?
by pike (Monk) on Nov 23, 2001 at 15:03 UTC
    'die' concatenates its argument like print does. This is so that you can say e.g.

    die "Expected ", scalar (@ref), " arguments, got ", scalar (@arr), "\n +";

    rather than having to explicitly concatenate the strings:

    die "Expected " . scalar (@ref) . " arguments, got " . scalar (@arr) . + "\n";

    The downside to this is that the args are obviously also concatenated when you pass the to a handler.


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://126960]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2023-10-01 22:23 GMT
Find Nodes?
    Voting Booth?

    No recent polls found