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:
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.
$contact = Contact->new(id => 1); $contact->load; print $contact->address->street;
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!