PiyaPerl has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I have a piece of code which tries to execute a stored procedure. That stored proc gives errors sometime because of which my script fails. I need to add the the same in a eval block like : my $getval = eval {stored proc calling with $_}; Now i want that if the stored proc fails then instead of ending my program it should just go and take the next element in the array and proceed again with evaluating the stored proc. Tried using ||do and also if($@) etc. but couldn't get it to work. Kindly suggest something.

Replies are listed 'Best First'.
Re: Eval block for routine error handling
by hippo (Archbishop) on Nov 07, 2017 at 10:59 UTC

    If eval is too tricky or unwieldy then go for Try::Tiny (as recommended by Task::Kensho). Other modules in the Try::* space are available but I find that Try::Tiny fits my requirements well.

Re: Eval block for routine error handling (UPDATED)
by thanos1983 (Parson) on Nov 07, 2017 at 10:36 UTC

    Hello PiyaPerl,

    Sample of your code it would us to understand what you mean and what you are trying to do.

    Update: In general I think you are looking for Best practices for handling errors or how do i trap the error generated by perl.

    Update2: In case you do not want to use any modules you can use something like that:

    #!/usr/bin/perl use strict; use warnings; use feature 'say'; my @array = ("backup", undef, "backup"); sub backItUp { say "I am backing it up!" if ($_[0] eq "backup"); } foreach my $element (@array) { eval { die if !defined $element; backItUp( $element ); 1; } or do { my $error = $@ || 'Unknown failure'; warn "Could not backup - $error"; }; } __END__ $ perl test.pl I am backing it up! Could not backup - Died at test.pl line 14. I am backing it up!

    Although I would agree with fellow Monk hippo using a module like Try::Tiny or Try::Catch would be a better option. You might be asking why? Simply The standard method of using eval {}; if ($@) {} is often prone to subtle bugs, primarily that its far too easy to stomp on the error in error handlers. And also eval/if isn't the nicest idiom. from the documentation of the module TryCatch.

    Looking forward to your update, BR.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: Eval block for routine error handling
by 1nickt (Canon) on Nov 07, 2017 at 10:40 UTC

    Hi, a simple technique is to return true from the eval block on success and evaluate that.

    for ( @all_the_things ) { eval { blah($_); 1 } or next; }

    Hope this helps!


    The way forward always starts with a minimal test.

      To: 1nickt,

      To clarify things since 1nickt send a insulting private message saying that I copied his code as an update to my comment Re: Eval block for routine error handling (UPDATED).

      1nickt try to understand that most people they where not born knowing programming. When you have something in your mind and you know how to resolve a problem provide a full answer with a compiling code. Not everyone knows how to resolve everything just like you and part of code and assuming the OP can figure out the rest is not an answer. If I was the OP after your question I would be still confused on how to use your code.

      Next time that you even consider to spell my name in your mind wash your mouth with chlorine and then you can spell it. You should feel glad that you can hide behind a pc and say things like that. People like you destroy forums and make environments toxic see Stack Overflow for example.

      I will stop somewhere here and as I replied on you message in private do not waste my time any more.

      Thanos

      Seeking for Perl wisdom...on the process of learning...not there...yet!

        Intermediary here... I'm not defending anyone for the reasons you speak here (as I wasn't part of the out-of-band communications), but I would like to say that I've been pissed right off on people and taken the opportunity to blast them publicly. I later regretted it. I'm not saying you will or you need to, I'm just saying that what happens outside of the public eye should be scrutinized carefully before bringing it public.

        Again, not taking sides here, you could very well be right. You could very well be upset at outside things, or you could just not like Nick. I don't care about any of that. One thing I do care about is the quality of everything people post here, yourself included. Take a breather bro, sometimes people make mistakes, and due to the major difference in location of our audience here, things do tend to get a bit bent out of shape.

        Personally, I've had battles with people here private and public, but the main thing is that we all focus on the main cause; providing assistance for Perl. Hell, Nick and I have even had our share of heated exchanges directly to really put this in perspective. That said, I've also been in phone conversations with him, along with email and other outside communications and I know he's a good person who has everyone on this site's best interest at heart, and despite our differences of opinion, I'd classify as someone that if I was in his area on a trip, go out for a beer with.

        Point is, try not to let transient things that someone may do that piss you off, piss you off to the point where you're so angry (I am NOT one to talk here, but I am doing so anyhow). Let it go, bro... this is not SO (yep, it can sure as shit be toxic!), it's Perlmonks. We sort out our differences, avoid those who are *real* antagonizers, and continue on pouring out code and help for those in need. Just like you have been.

        I have ++'d your post, because I've felt the way you have, and I know what it's like to feel like I may have been shunned. Posting publicly like this leaves it to the reader to decide on the context of the situation (was it a quick one-off answer where the person didn't have time to fully explain, where there was hope that another Monk would step in to elaborate? Man, I do this often!). Was he really trying to call you out for something else? We don't know, and really shouldn't know unless it's bad enough that warrants a /msg to the gods or the like.

        My point is, that negative posts publicly toward someone else aren't necessarily the best approach. I am guilty of this, and I know for fact in my ~2,200 messages, I have a few apologies to match them. For me, I felt I was wrong. You may not, so the situations may not be the same. That said, my advice is to carry on carrying on, and don't let the little things bother you. There are bigger fish to fry, and bigger fish that probably could fry you.

        Cheer up, and know that everyone makes mistakes, everyone gets pissed off, everyone says things they don't mean periodically, everyone has beefs, everyone has bad days, everyone has times they can't provide a full-blown explanation for a solution, and the most important fact is that everyone here has the opportunity to move forward with a positive attitude toward helping others, all problems aside.

        Move along, keep posting the way you have, and all will be well ;)

        -stevieb

        G'day Thanos,

        "... insulting private message ..."

        Here's some actions that would keep such messages "private". They are not presented in any particular order; nor is there any suggestion that they are mutually exclusive.

        • Take no immediate action beyond deleting or archiving the message.
        • Stop more messages with /ignore.
        • Attempt to resolve the issue via private messaging.
        • Choose to continue the fight and trade insults via private messaging.

        As a last resort, read about "court of last appeal" in gods: this would now involve a few other people but otherwise still keeps it private.

        What you definitely should not do is respond to a derogatory private message with a derogatory public message. The fact that you did this was my motivation for this response: I have no interest in people's private squabbles.

        [I realise english isn't your first language, and maybe suggesting putting chlorine in one's mouth was an unfortunate translation of an idiom from your native tongue; however, chlorine is poisonous, it's been used as a chemical weapon in warfare, and putting it in your mouth can cause serious injury or death. I don't believe you were advocating self-harm or suicide.]

        In closing, I'm definitely not taking sides in this, and have purposefully not voted on any of the nodes in this sub-thread.

        — Ken

A reply falls below the community's threshold of quality. You may see it by logging in.