Perl SQLite error handling: performing various actions according to the error code


I am new to Perl programming (and for SO too), so my question may not be formulated correctly, but I really read a lot of books and manuals and I did not find anything that concerned (even mentioning) my problem.

I am trying to use DBI and SQLite to write code that repeats the insertion request if a recovery error occurs (full or locked DB, etc.), but dies if the error is unrecoverable (the database is deleted or damaged, etc. )

I found that the SQLite C interface is exporting error codes:

http://www.sqlite.org/c3ref/c_abort.html

but I did not find anything like it for Perl. I'm sorry that I don't need to use magic numbers in my first Perl program !:-)

By the way, the documents and examples that I saw on the Internet very well explain manual processing errors or automatic (i.e. with exceptions) in DBI, but none of them show how to perform different actions according to type mistakes. Is this not an ordinary case? Moreover, they all agree that DBI :: err is not a valid variable to report which error occurred. They more or less implicitly say that DBI :: errstr should be used, but I find it a little awkward to rely on comparing strings with a human-oriented, perhaps multi-line error string ...

Thanks for any suggestion!

+4
source share
1 answer

My work with DBI was almost always with mysql instead of sqlite3, so I can not speak from direct experience. However, do not feel too bad if you absolutely must check the magic lines and numbers. The main thing is how you do it. Whenever you have to rely on magic strings and / or numbers, put them in a constant or hash in the configuration section of your script (or perhaps even a configuration file).

The bad thing about magic numbers / strings is that it is difficult to control if they change, or no one remembers what this condition generates, etc. But this is mitigated if you do it right and document it.

BTW - if you are just getting started, I highly recommend reading Damian Conway's, "Perl Best Practices". I checked and it doesn’t address magic line / number processing, but it is still the best book I read in Perl. After you get through PBP, take a look at Perl Critic, a great tool to make you cry. It will indicate magic lines and many, many other things :)

+1
source

All Articles