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;The first thing we do is include our MyApp::Contact module with:
$u = MyApp::Contact->new(id => '1');
$u->load;
print "User's name is ".$u->name.", email address is ".$u->email.".\n";
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');This could just as easily load them based on the email address, or any other field like so:
$u->load;
$u = MyApp::Contact->new(email => 'larry@thestooges.com');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:
$u->load;
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.
