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
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |