I'm a beginning-intermediate Perl programmer and am currently working on a pretty simple DBI-based program. The focus of my question, however is not DBI-specific. Rather, it's a question about using (I believe) anonymous subroutines. What I want to do is catch it when one of my DBI statements fails. Depending on the error that caused the failure, I may want to just continue on or I may want to quit. My question is two-fold:
Code follows, with questions in the comments:
# This is just an insert statement. What I want to do # is catch any errors, and then vary my actions based # on the error. Can I do this better? $newSeriesSth->execute() || &{ # is this an anonymous sub? if( $newSeriesSth->errstr =~ /duplicate/ ) { # this error is okay, i don't want to quit print "Trying to insert duplicate row.\n"; } else { # some other error, better safe to quit now. die("Unhandled error. DBI says: " . DBI->errstr); } };
Now, given that this is acceptable programming (which it may not be), the special bonus question is why do I get this error message while running this code?
Can't use string ("1") as a subroutine ref while "strict refs" in use at line 167 (Line 167 is the if(..) statement).
~CubicSpline
"No one tosses a Dwarf!"
In reply to Anonymous sub as error handler by CubicSpline
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |