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.
In reply to Re: Eval block for routine error handling (UPDATED)
by thanos1983
in thread Eval block for routine error handling
by PiyaPerl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |