Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use autodie with non-builtins?

Tags:

perl

autodie

The autodie documentation hints that it is possible to use it for other functions than those built-ins which it can handle by default, but there are no clear examples how to do that in it.

Specifically I would like to use it for the Imager module. A lot of the functions and methods of that can fail, and I would prefer if that wouldn't mean that my code will be littered with or die Imager|$image->errstr; phrases all over.

Of course, if there's another way than using autodie to achieve that, I would be interested in that too.

like image 251
Rörd Avatar asked Mar 17 '11 10:03

Rörd


1 Answers

autodie only works with functions, not methods. This is because it's lexically scoped, and method lookup can't be lexically scoped. autodie::hints explains how to tell autodie about user-defined functions, but that won't do anything for methods.

I don't know of any way to get autodie-like behavior for methods, unless the module has that built in (e.g. DBI's RaiseError).

You could have a subroutine to do the check, but it wouldn't save all that much code, since you'd still have to pass it the correct object or class to call errstr on.

like image 189
cjm Avatar answered Oct 02 '22 22:10

cjm