You can see that I could use eval (commented out) but again, I am unsure of the best practice!?
That's how I'd do it: die (or croak) in your module if necessary, and have the caller wrap the call in question in an eval to catch the exception (since that's what it really is).
Of course this can get cumbersome, so if your subroutine can only return certain values (e.g. only values that are true), you could also return false or undef, and have the caller invoke it like this:
$splat->splat() or die "splat() returned false";
unless(defined $splat->splat()) {
die "splat() returned undef";
}
(Hmm, it sure would be nice if perl had a defined-or version of the or operator.)
Whether this is feasible depends on what splat() is supposed to be return, of course. For numbers there's a trick: you can return a string along the lines of "0 but true"; this'll numify to zero, but evaluate to true in boolean contexts. For strings, I don't think there's any such trick for the empty string.
There is no single best solution; it's a matter of taste.
|