Working with Databases in Perl
Introduction
There are many types of databases:
- If you want to work with databases based on SQL (Structured Query Language), then refer to the SQL databases section of this page.
- If you want to use a DBM database (implementing keys→values dictionaries or mappings inside a file) refer to our DBM databases section.
- If you want xBase (dBase / Clipper / FoxBase) databases refer to our xBase section.
- If you want a distributed database (similar to Google's BigTable), see our distributed databases section.
SQL Databases
The de-facto standard for working with SQL databases in Perl is DBI (short for Database Interface). It provides a unified way to perform SQL queries across many database backends: PostgreSQL, SQLite, MySQL (but see this page for some reasons why you may prefer a different system), Oracle, Firebird, and others.
Above DBI, people wrote some useful abstractions:
DBIx-Simple - an easy to use Object-Oriented Interface to DBI.
DBIx-Class - a powerful and convenient object-relational mapper. Use of the older and less philosophically sound Class-DBI is heavily discouraged.
Rose-DB-Object - another ORM - seems to be much less popular than DBIx-Class, but still actively maintained.
When working with databases make sure you avoid common pitfalls such as SQL injection vulnerabilities.
DBM Databases
- The BerkeleyDB CPAN module is the standard module for working with Oracle Berkeley DB (formerly Sleepy Cat Berkeley DB) and maps the ANSI C library's API closely. Note that Berkeley DB is licensed under the Sleepycat License which is strong copyleft, and does not allow use for non-open-source programs.
- Tokyo Cabinet - a modern, fast and powerful DBM implementation available under the LGPL license (which permits use in non-open-source projects). There’s also Kyoto Cabinet, which is claimed to be better, but is under the GPL license (which is strong copyleft and does not permit use in non-open-source projects).
- LevelDB from Google, a fast key-value storage library, open-source under the new BSD license (which allows use for non-open-source programs. Has Perl bindings on CPAN called Tie-LevelDB.
- DBM-Deep - a multi-level hash/array DBM that supports transactions. Reported to be slow, however.
XBase Databases
There seems to be an XBase module on CPAN . It seems to be pure-Perl, so it may be slow.
Distributed Databases ("No SQL", etc.)
Recently, to meet the growing demand for data of some web sites, some people have switched to using non-centralised, non-SQL based databases that use a cluster of servers to implement a distributed data storage. This has been dubbed "No SQL". You most likely would prefer to use an SQL-based solution, which should be easily able to handle the data in your scope, but here is a list of some bindings for distributed databases just for completeness sake.
- Net-CouchDb - an interface to Apache's CouchDb.
- MongoDB - an interface to MongoDB.
Links
- "Databases and mod_perl" part from Practical mod_perl - provides many useful insights.