1. Home
  2. Computing & Technology
  3. Perl

Adding Object-Relational Mapping to your Perl Application
Installing and Configuring Rose::DB

by Kirk Brown
for About.com

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?

Explore Perl
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Perl
  4. Working With Databases
  5. A tutorial on using Rose::DB and Rose::DB::Object to add Object-Relational Mapping to your Perl application.>

©2009 About.com, a part of The New York Times Company.

All rights reserved.