Adding Object-Relational Mapping to your Perl Application

106 7
< Continued from page 1

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?
Source...
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.