Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Can I catch exceptions

by iakobski (Pilgrim)
on Jun 26, 2001 at 15:30 UTC ( [id://91558]=note: print w/replies, xml ) Need Help??


in reply to Can I catch exceptions

The first two answers are correct answers to your question, however they will not solve your problem.

What you are seeing is not an exception or an error, it is a warning. A subtle difference perhaps, but the eval and die technique will not trap warnings.

What you need to do is to temporarily modify __WARN__ so that instead of the default, which is to print out the warning, it does what you want. You say that you want to catch the "error" and generate your own message. You could do something like this:

sub my_comparison{ @_ == 2 or die "Incorrect number of args to my_comparison"; local $SIG{__WARN__} = sub { print "Tried to test a non-number with +==\n" }; local $^W = 1; $_[0] == $_[1]; }
Note that you MUST enclose this in a block (such as putting it in it's own sub like this) otherwise you will be confused by any other warnings later printing your own message.

-- iakobski

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-03-29 11:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found