Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
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 having an uproarious good time at the Monastery: (14)
As of 2015-07-07 13:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (88 votes), past polls