Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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 ./ line 12. ( 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 ./ line 20.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://976804]
[LanX]: "DH" eq "Dumb Homies "?
[shmem]: no, no previous, and python just accepts that.

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (13)
As of 2018-03-20 18:24 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (257 votes). Check out past polls.