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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|