[EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
> Howdy,
> 
> If I'm handling errors with
> eval {} if ($@) {}
> constructs, is there any hidden gotcha to grouping several statements in a 
> single eval if I want any errors handled identically?  For instance, can I wrap 
> an execute and its fetches into one eval, as below, or is this something I 
> should be wary of?
> 
> Paul
> 
> # group an execute and its fetches into one eval
> eval {
>     $sth->execute();
>     while (my $ref = $sth->fetchrow_arrayref()) {
>           print OUT join("\t",  @$ref), "\n";
>           }
>     };
> if ($@) {
>     $dbh->rollback();
>     close(OUT);
>     print "Error retrieving data\n";
>     exit();
>     }
> 
> # wrap the execute and each fetch individually
> eval {
>     $sth->execute();
>     };
> if ($@) {
>     $dbh->rollback();
>     close(OUT);
>     print "Error retrieving data\n";
>     exit();
>     }
> 
> while (1) {
> 
>     my $ref;
> 
>     eval {
>          $ref = $sth->fetchrow_arrayref();
>          };
>     if ($@) {
>          $dbh->rollback();
>          close(OUT);
>          print "Error retrieving data\n";
>          exit();
>          }
> 
>     print OUT join("\t",  @$ref), "\n";
> 
>     }

Paul, you normally don't care about committing or rolling
back unless you're actually updating the database by doing
either an INSERT or an UPDATE.  When you're just doing
SELECT and fetches you really don't care about transaction
processing.  If you want to use eval and $@ to trap errors
with fetches that's fine, but no need to have the rollback
in the if ($@).

I can't really think of any gotchas to be wary of using
eval to wrap several dbi statements.

-- 
Hardy Merrill
Red Hat, Inc.

Reply via email to