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

I know that in string context $! will print an error message (under the correct circumstances):

open(...) or die "Oops: $!";

I was wondering if that is the same string or if it is different from the value passed to a signal handler written to handle a SIGWARN/DIE:

$SIG{WARN} = sub { print "doh: ".$_[0]; return; }

Replies are listed 'Best First'.
(tye)Re: $! vs the @_ passed to your SIG(DIEWARN) handler
by tye (Sage) on May 05, 2001 at 02:23 UTC

    $! is really just errno and so can only be set to a finite number of string values. For example,

    $!= "Oops"; print $!;
    prints nothing because the first line sets $! to 0 and the text associated with errno == 0 is the empty string. So $! can't (in general) be the same as the error message string that gets passed into a __WARN__ handler.

    See "man strerror" (or your C compiler's documentation) for more on what ends up in $!.

            - tye (but my friends call me "Tye")
Re: $! vs the @_ passed to your SIG(DIEWARN) handler
by stephen (Priest) on May 05, 2001 at 01:14 UTC
    A warn or die handler is passed the argument to either warn or die. So if you have:
    $SIG{__DIE__} = sub { print "Died with message '$_[0]'\n"; }; open(NOFILE, '/no/file/here') or die "Ooops: $!";
    then you will get:
    Died with message 'Ooops: No such file or directory at /home/stephen/t +est.pl line 2. '

    Make sure you don't forget the double underscores before and after WARN and DIE, or you won't catch anything at all.

    See perlman:perlvar for more information on %SIG. See perlfunc:warn and perlfunc:die for trapping warn and die.

    stephen