Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Catching DBI (or other) errors

by nmerriweather (Friar)
on Mar 08, 2006 at 09:47 UTC ( #535129=perlquestion: print w/replies, xml ) Need Help??

nmerriweather has asked for the wisdom of the Perl Monks concerning the following question:

I'm having an issue w/catching DBI errors

I do the following:
$query->execute() || { $SQL_FAIL = 1 };
which does exactly what i want - sets a var if the sql command fails, so I can handle the error with more granularity than handler error

The problem i've run into is this: the {} creates a hashref warning , which i'd like to get rid of . can anyone offer a suggestion?

Replies are listed 'Best First'.
Re: Catching DBI (or other) errors
by clinton (Priest) on Mar 08, 2006 at 11:17 UTC
    You could write it as

    $query->execute() or do { $SQL_FAIL = 1};
    But for a single line statement, you really don't need the braces, just :
    $query->execute() or $SQL_FAIL = 1;
    But why are you doing it this way?

    This means that you then have to check the value of SQL_FAIL every line before processing the next statement.

    Wrapping it all in an eval and using RaiseError really works a treat, looks clean makes your life simpler.

    If you are using the value of SQL_FAIL to figure out what statement failed, then you could go the other way and have $sql_stage (note, not all in capitals because this implies a constant, which it isn't) and you could just set $sql_stage to the current stage you're working on. As in
    my $sql_stage=1; eval { $query->execute(); $sql_stage = 2; $query2->execute(); $sql_stage = 3; $query3->execute(); }; if ($@) { if ($sql_stage < 3) { $fix1->execute(); } else { $fix2->execute(); } }
      If I were going to do something like this, I'd probably do something more like:
      $query->execute and $sql_stage++; $query2->execute and $sql_stage++;
      etc.

        That would execute all queries even if one of them failed though. And if after your code finishes, $sql_stage is 1, you still don't know whether it was $query or $query2 that failed. Now add 3 or 4 more queries to that list and the $sql_stage variable becomes practically useless for anything except comparing to the total number of queries.

Re: Catching DBI (or other) errors
by virtualsue (Vicar) on Mar 08, 2006 at 11:24 UTC
    I suggest that you either replace the curly braces with parentheses, or use 'or' rather than '||':
    $query->execute() || ($SQL_FAIL =1); $query->execute() or $SQL_FAIL =1;
    'or' has a lower precedence than '=', but '||' has a higher precedence, which is what makes parentheses necessary in your original statement.
Re: Catching DBI (or other) errors
by nmerriweather (Friar) on Mar 08, 2006 at 21:51 UTC
    thanks to all.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://535129]
Approved by virtualsue
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2023-01-28 23:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?