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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.