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!

In reply to Re: Eval block for routine error handling (UPDATED) by thanos1983
in thread Eval block for routine error handling by PiyaPerl

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.