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

Re^6: Use of uninitialized value $pic

by dazz (Beadle)
on May 01, 2017 at 10:58 UTC ( [id://1189249]=note: print w/replies, xml ) Need Help??


in reply to Re^5: Use of uninitialized value $pic
in thread Use of uninitialized value $pic

Hi
I don't know. What does this do:
warn "$x" if "$x";
Google tells me it's a warning. Undef??

dazz

Replies are listed 'Best First'.
Re^7: Use of uninitialized value $pic
by afoken (Chancellor) on May 01, 2017 at 11:03 UTC

    It's a very ugly hack for error checking. See "Handling Exceptions" in the documentation of Image::Magick. Sane error handling would use die or the XS equivalent of die inside the Image::Magick code, but now it's too late for an incompatible change.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      Hi
      This is running on a headless system. Does that mean I should use:
      my $iutImage = $_[0]->Clone(); warn "$x" if !ref($x); # print the error message $x =~ /(\d+)/; syslog(LOG_ERR, "Attempt to clone image failed. Error: $1"); # log + the error number

      If the "warn" command sends error messages to the terminal, how do I send those messages to syslog??

      Ref: https://www.imagemagick.org/script/perl-magick.php#exceptions

        Another note:

        There is an important difference between warn and die.

        warn just warns, but continues. This is obviously intended for warnings. Stuff that went wrong, but can and will be automatically fixed by the program. Something like "configuration file not found, will use defaults".

        die aborts the program due to an error. Any statements following die won't be executed. As obviously as before, this is intended for errors. Stuff that went so wrong that the program can not fix the problem and has to abort. Something like "Input is not a valid file, can't continue".

        The Clone() method generates a clone of the image on which you want to work. If Clone() fails, you have nothing to work on. This is a clear case for die, not for warn.


        die has a second purpose, very similar to the first one: Exception handling.

        Java (and C++, C#, Python, ...) people would perhaps write something like throw new FooBarException("bla bla") in a library and even in the main program. Perl people use die in the main program and also in the libraries. Java people use try and catch to handle exceptions, in Perl, you use eval (in the BLOCK variant) and check $@ if eval fails. eval prevents die from aborting the program, it just aborts the eval block.

        The combination of eval and die has some nasty corner cases, so the usual idiom is to have eval return a true value and only check $@ if eval returns false:

        unless (eval { # <-- "try" do_something_that_might_die(); 1; }) { # <-- "catch" my $error=$@; # check $error # then die or fix / workaround the problem }

        Several libraries, like Try::Tiny and TryCatch, try to make that look a little bit nicer, with try and catch keywords.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

        warn, like die, writes to STDERR. Redirecting STDERR to logger would write to syslog.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      Never too late for a compatible change 😊 deja vu

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1189249]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-25 23:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found