Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Method references

by Neighbour (Friar)
on Jun 18, 2012 at 10:24 UTC ( #976804=note: print w/ replies, xml ) Need Help??


in reply to Method references

A standard way to catch errors is to use die (or Carp), in combination with eval. But since I prefer Try::Tiny, I'm going to use that.
For example:

#!/usr/bin/perl use strict; use warnings; use Try::Tiny; $main::errorA = 1; $main::errorB = 1; sub methodA { if ($main::errorA) { die "Something went wrong in methodA"; } } sub methodB { my ($handle) = @_; &$handle(); if ($main::errorB) { die "Something went wrong in methodB"; } } ## end sub methodB my $rowHandle = \&methodA; try { methodB($rowHandle); } catch { print ("ERROR: " . $_); }
This results in:
ERROR: Something went wrong in methodA at ./monks24.pl line 12. (monks24.pl being the name of the executable).
This is because errorA is checked before errorB.
If you'd set errorA to 0 (meaning all is well in methodA), you'd get this:
ERROR: Something went wrong in methodB at ./monks24.pl line 20.


Comment on Re: Method references
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2014-12-18 04:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (41 votes), past polls