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

Re: Tying events to perl warnings and errors

by Shendal (Hermit)
on Sep 13, 2000 at 23:00 UTC ( [id://32323]=note: print w/replies, xml ) Need Help??


in reply to Tying events to perl warnings and errors

You can trap any warn (or die) yourself like this:

use strict; $SIG{__WARN__} = \&mywarn; sub mywarn { print "here!\n @_\n"; } { test ('arf', undef, 'dog'); } sub test { @_ == 3 or die "Incorrect number of arguments"; my ($a, $b, $c) = @_; my %hash = ('arf' => {'spot' => {'dog' => 3}}); print $hash {$a}->{$b}->{$c}; }
...which prints this output:
here! Use of uninitialized value in hash element at test.txt line 23. here! Use of uninitialized value in hash element at test.txt line 23. here! Use of uninitialized value in print at test.txt line 23.

Hope that helps,
Shendal

Replies are listed 'Best First'.
RE: Re: Tying events to perl warnings and errors
by Adam (Vicar) on Sep 13, 2000 at 23:37 UTC
    Playing with $SIG{__WARN__} and $SIG{__DIE__} is fun, but remember that you can't out-wit death. If die gets called, the script will end, but first it will evaluate/run the expression/subroutine that is $SIG{__DIE__}. It will pass die's arguments in as @_ and will disable the signal (avoiding recursive calls to $SIG{__DIE__}). Good stuff really. Great if you want to say... print a time stamp whenever your script dies... or log warnings to an extra log file. (you trigger __WARN__ when you call warn too.)
        Thanks.
(jcwren) Re: (2) Tying events to perl warnings and errors
by jcwren (Prior) on Sep 14, 2000 at 00:17 UTC
    The only problem with the $SIG{__WARN__} method is that I don't get the data I want. I'm not a perlguts guy, so maybe someone can point the way for what I mentioned I wanted in the article, which is access to the variable list for the subroutine.

    What I want to do is upon receipt of the error, dump the variable state for the subroutine out to disk. Timestamping is all well and fine, but it doesn't get me the information that I'm really needing. Any ideas?

    --Chris

    e-mail jcwren

      Localize the handler with a closure, like this:

      sub foo { my ($a, $b, $c) = @_; my %hash = (a => {b => {c => 0}}); # whatever other variables you want to initialize local $SIG{__WARN__} = sub { # dump those variables here }; print $hash{$a}->{$b}->{$c}; # ... }

      The variables you want to dump will be in scope of the __WARN__ handler and you can do whatever you like with them.

      local $_ = "0A72656B636148206C72655020726568746F6E41207473754A"; while(s/..$//) { print chr(hex($&)) }

RE: Re: Tying events to perl warnings and errors
by geektron (Curate) on Sep 13, 2000 at 23:37 UTC
    this method should work out.

    i'd also suggest logging to syslog with Sys::Syslog rather than writing out logfiles. that way, any permissions issues are avoided, and no one can remove/replace the log files.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://32323]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (None)
    As of 2024-04-25 00:47 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found