Perl

  1. Home
  2. Computing & Technology
  3. Perl

Adding Object-Relational Mapping to your Perl Application - Part Three

Using Rose:DB in Your Application

From About.com

Next: Adding New Records to the Database

In our last two tutorials, we first installed and configured Rose::DB and then we setup and created a Rose::DB object. To finish off this series, lets take a look at how we actually use our Rose::DB object inside our Perl programs. This is where it gets fun!

The following is a simple example of loading and reading your new Contact object:

use MyApp::Contact;

$u = MyApp::Contact->new(id => '1');
$u->load;

print "User's name is ".$u->name.", email address is ".$u->email.".\n";
The first thing we do is include our MyApp::Contact module with:
use MyApp::Contact;
This pulls in all the Rose::DB code that we installed and configured in the last two tutorials. Next we're going to load one of the contacts we created for our sample database by it's unique ID (#1):
$u = MyApp::Contact->new(id => '1');
$u->load;
This could just as easily load them based on the email address, or any other field like so:
$u = MyApp::Contact->new(email => 'larry@thestooges.com');
$u->load;
Once the record from the database is load()ed into the object we can view it as though each row is in fact an attribute of the object:
print "User's name is ".$u->name.", email address is ".$u->email.".\n";
That should, of course, print the statement User's name is Larry, email address is larry@thestooges.com.

Next: Adding New Records to the Database

Explore Perl

About.com Special Features

Perl

  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.