Perl: the Markov chain saw | |
PerlMonks |
Re^3: Recalcitrant placeholdersby afoken (Chancellor) |
on Jul 06, 2021 at 21:21 UTC ( [id://11134729]=note: print w/replies, xml ) | Need Help?? |
my $crid = $crm->db->selectrow_array$crid is undef Why am I not surprised? Let's RTFM: selectrow_array Let me shorten that a little bit for weary eyes:
Now, error handling. DBI offers a tedious, error-prone and highly annoying way of error handling, and a smart and lazy way of error handling. The tedious way is to add an extra error check to almost any DBI method call. Just like you would have to do in assembler or in C. You need to study the DBI documentation to find out how each method behaves on error. And yes, they do behave differently. Or, you could have DBI and perl do all of that grunt work for you, by just adding RaiseError => 1 to the attribute hash of connect(). That automatically adds error checks to all methods, and all methods will handle errors in the same way: They die instead of returning special error indicator values. And that's almost always exactly what you want. It's also extremely useful for doing transactions, see "Transactions" in the DBI documentation. This is copied right out of that section:
(You could also do without Try::Tiny, by using an eval BLOCK that returns a true value:)
There are rare cases when you expect an SQL statement to fail (like testing if a table exists by simply using it in an SQL statement). Wrap them into eval BLOCK or try/catch and you are done. And finally, doing database stuff in a web context with RaiseError set. Yes, an uncaught (i.e. unhandled) database error will kill your program, resulting in an ugly "500 Internal Server Error" or something similar. That's a good thing. It tells you that your code still has errors. As I wrote: If you expect database errors, wrap them in eval BLOCK or try/catch. For a simple-minded CGI, wrapping all of the database stuff in a big eval BLOCK and returning a "database oopsie" page on error is often acceptable. Alexander
-- Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
In Section
Seekers of Perl Wisdom
|
|