Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Fellow Monks,

Please shine your blessed light on this:

I was writing some code using Image::Magick and found myself repeating lines like:

$err = $image->DoSomething; if("$err") { # one must evaluate the return value # in string context, according to the # PerlMagick docs die ("Can't do Something: $err\n"); }

Which I find less than esthetical for several reasons. And even now I::M sometimes printed a warning in stead of giving an error message.

I'd rather do:

eval { $image->DoSomething; $image->DoSomethingElse; $image->Save; }; if($@) { handle_the_error; }

So I started Image::Magick::Exceptions, borrowing from Simon Cozens' Class::Wrap:

package Image::Magick::Exceptions; use warnings; use strict; use Exception::Class qw/Image::Magick::Exception/; { no strict; no warnings qw/redefine prototype/; my $class = "Image::Magick"; for my $method (keys %{$class."::"}) { my $cmethod = "${class}::$method"; if ( defined *{$cmethod}{CODE} and $method ne 'new' and $method !~ /^[A-Z]+$/ ) { *{"hidden::$cmethod"} = *{$cmethod}{CODE}; *{$cmethod} = sub { my $err = &{"hidden::$cmethod"}; if("$err") { Image::Magick::Exception->throw (error => "$err"); }; $err; }; } } } package main; use Image::Magick; use Image::Magick::Exceptions; etc();

As a proof of concept this works. BUT, things start going wrong when a function is *supposed* to return a string, like the Get/GetAttribute (why all these aliases, by the way?). Reading the documentation again, it says you need to handle methods differently based on what they are supposed to return.

Numerical methods should be tested like:

$ret = $image->Write; if ("$ret") { error("$ret") } print $ret . " images written";
Here, evaluating $ret in string context apparently returns something different than evaluating $ret in numerical context.

"Void" methods can be tested the same. If the return value is not empty, there's an error.

Image operations must be tested like:

$clone = $image->Clone; if (!ref $clone) { error($clone) }

And the documentation fails to mention functions that are *supposed* to return string values.

I could fix my Image::Magick::Exceptions module, by keeping a list of method names and return types.

But where to find these? Even the I::M manual is not clear on what methods return what. Or even fails to mention all methods.

And even this doesn't handle the warn()'s that I::M throws every now and then.

Finally, and thanks for bearing with my long winded post, the question:

Is there a better (non-documented) way to catch errors from I::M?
Or else, is there a better manual than the one provided with I::M?

--
Lyon


In reply to Exception handling for Image::Magick by redlemon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-24 21:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found