can you convert this python code to perl code (i search about it but dont find a good result)
for i in xrange(20):
try:
instructions
except :
pass
Thanks
can you convert this python code to perl code (i search about it but dont find a good result)
for i in xrange(20):
try:
instructions
except :
pass
Thanks
You should look at the Try::Tiny module. Also your question really should be fleshed out some more. You don't give us any info at all to help you. What problem are you trying to solve by doing this?
As far as I know it's not built into perl, but there is at least one module for implementing try/catch behaviour
http://search.cpan.org/~nilsonsfj/Error-TryCatch-0.07/lib/Error/TryCatch.pm
use strict;
use warnings;
use Try::Tiny;
for my $i (0 .. 19){
try {
print "1 / $i = ", 1 / $i, "\n";
}
catch {
print "Doh! $_";
}
}
There are packages that provide better exception handling then is directly implemented in the language, but without using anything like that, you'd do something like this:
foreach my $i (0..19) {
eval {
# instructions
print $i;
if ($i == 5) {
die("boo");
}
1;
} or {
# exception handling
print $@;
}
}
So you die
instead of raise
in an eval block, and then examine the $@
variable afterwards to see if an error was thrown.
I would recomend writing an exception class to throw though, instead of using strings.