G'day rajuskark,

"I am using eval block many time in my program is. I want to know is there any harm or any drawbacks if using it many times"

There are drawbacks to using eval that are not related to the number of times it is used. See

Whether these drawbacks apply to your code will depend on what all the various '----'s are, as well as whatever code surrounds your while loop. As none of that is shown, you'll need to determine this for yourself.

Your use of multiple 'eval{----};if($@){-----}' looks clunky and may be entirely unnecessary; that is, you may be able to use just one 'eval{----};if($@){-----}'. Here's a sample script to show how that might be achieved.

#!/usr/bin/env perl use strict; use warnings; use autodie; my @ids = 0 .. 3; while () { eval { my $id = pop @ids; die $id if $id; open my $fh, '<', 'pm_1146365_nonexistent_file'; }; warn "TRAPPED: $@" if $@; last unless @ids; }

When run, this produces:

$ pm_1146365_multi_eval.pl TRAPPED: 3 at ./pm_1146365_multi_eval.pl line 12. TRAPPED: 2 at ./pm_1146365_multi_eval.pl line 12. TRAPPED: 1 at ./pm_1146365_multi_eval.pl line 12. TRAPPED: Can't open 'pm_1146365_nonexistent_file' for reading: 'No suc +h file or directory' at ./pm_1146365_multi_eval.pl line 13

See also: the autodie pragma and the croak and confess functions of the Carp module.

— Ken


In reply to Re: Drawback of eval {---}if ($@){---}, when using many times? by kcott
in thread Drawback of eval {---}if ($@){---}, when using many times? by rajuskark

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.