Adding Object-Relational Mapping to your Perl Application

What is Object-Relational Mapping?

Man at table looking at laptop computer screen
John Howard/Photodisc/Getty Images
What is Object-Relational Mapping?

Next: Installing and Configuring Rose::DB

Object-Relational Mapping is a method of mapping a relational database to a class structure in an object-oriented programming language. In simpler terms, it takes your database structure and creates an interface that keeps you from having to write any SQL at all. Not only does it abstract the database, it abstracts the relational portion as well, making it simpler for you to work in an application.

Say for example you have a database with two tables, Contact and Address, that are joined via the contact's ID. Using standard PERL::DBI to load a contact's street address, you would perform a join statement in the SQL, return a row in the database, and then display the street address column somehow at the end. With Object-Relational Mapping, this becomes as simple as:

 $contact = Contact->new(id => 1);
 $contact->load;
 print $contact->address->street; 
There are several advantages to this method. First is the abstraction - using ORM means that changes to the data structure and even the underlying database can be made in the background while limiting the effect on the application itself. Another is code simplicity - pushing the SQL into the ORM layer of your application means that future maintenance will not require in-depth knowledge of SQL or your particular database. Yet another is keeping your application Object Oriented in structure by representing data as objects rather than tables in a database.

This type of Object-Relational Mapping has been around for quite some time, but has recently gained popularity in large part due to Ruby on Rails and it's ORM layer, ActiveRecord.

Perl has many different plugable modules for ORM, but the one we'll be working with is called Rose::DB::Object. In order to get it up and running we'll need a few things, so let's get started installing and configuring Rose::DB!

Next: Installing and Configuring Rose::DB

In order to work with Rose::DB::Object, we first need to install the CPAN module Rose::DB and the driver for our database, which is Rose::DB::MySQL in this example. If you've forgotten the basics of installing modules from CPAN now would be a good time to refresh your memory with the tutorial! You'll need to install both Rose::DB and Rose::DB::MySQL from CPAN, along with all their dependencies.

Next, lets get our application's database setup configured and running.

For the purposes of this tutorial, we're going to drop all our files into a single application module called MyApp. This will keep it all clean and neat. In accordance with standard Perl package setup, you'll need to create a directory called MyApp within your Perl include path, typically /usr/lib/perl5 or something similar.

Inside this directory, let's create our database configuration file called DB.pm with the following settings:

 package MyApp::DB;
 
 use Rose::DB;
 our @ISA = qw(Rose::DB);
 
 __PACKAGE__->use_private_registry;
 
 __PACKAGE__->register_db(
 driver => 'mysql',
 database => 'my_db',
 host => 'localhost',
 username => 'my_user',
 password => 'my_pass'
 ); 
First is the package declaration, then we pull in the Rose:DB files. Next we create a private registry for our Rose::DB class that keeps it isolated from any other setups. Last we have our actual configuration settings for the database. The driver in this case is MySQL, but could be any of the compatible drivers (postgres, etc). The rest - database, host, username, and password are standard fare for database configuration.

That's all we need to get started with a basic Rose::DB application. Now we can include our custom MyApp::DB class in a script like so:

 use MyApp::DB; 
This is the first step towards using Rose::DB::Object and setting up our Object-Relational Mapping.

Previous: What is Object-Relational Mapping?