Class::PObject - Perl extension for programming persistent objects


We can create a person object like so:

  use Class::PObject
  pobject Person => {
    columns => ['id', 'name', 'email'],
    datasource => 'data/person'

Or even:

  package Person;
  pobject {
    columns => ['id', 'name', 'email''
    datasource => 'data/person'

The above Person can be now used like so:

  my $person = new Person();
  my $new_id = $person->save();

We can read the above saved Person later, make necessary changes and save back:

  $person = Person->load($new_id);
  $person->name('Sherzod Ruzmetov (The Geek)');

We can load all the objects with the following code:

  my @people = Person->load();
  for ( my $i=0; $i < @people; $i++ ) {
    my $person = $people[$i];
    printf("[%02d] %s <%s>\n", $person->id, $person->name, $person->email);

or we can load all the objects with name 'Sherzod' and sort the list by name in descending order, and limit the results to only the first 3 objects:

  my @people = Person->load({name=>"Sherzod"}, {sort=>'name', direction=>'desc', limit=>3});

We can also retrieve records by offset:

  my @people = Person->load(undef, {offset=>10, limit=>10});


This is 'alpha' release. I mainly want to hear some ideas, criticism and suggestions from people. Look at TODO section for more details.


Class::PObject is a class representing a persistent object. Such objects can be stored into disk, or even initialized from the disk.

If it is easier for you, just think of a persistent object as a single record of a relational database:

  mysql> select * from song;
  | id  | title          | artist | album_id |
  | 217 | Yagonam O'zing | Sevara |        1 |

The above record of a song can be represented as a persistent object. Using Class::PObject, you can defined this object like so:

  pobject Song => {
    columns => ['id', 'title', 'artist', 'album_id']
  my $song = new Song(title=>"Yagonam O'zing", artist=>"Sevara", album_id=>1);

All the disk access is performed through its drivers, thus allowing your objects truly transparent database access. Currently supported drivers are 'mysql', 'file' and 'csv'. More drivers can be added, and I believe will be added.


The style of Class::PObject is very similar to that of the Class::Struct manpage. Instead of exporting 'struct()', however, Class::PObject exports 'pobject()'. Another visual difference is the way you define your arguments. In Class::PObject, each property of the class is represented as one column.


Object can be created in several ways. You can create the object in its own .pm file with the following syntax:

  package Article;
  pobject {
    columns => ['id', 'title', 'date', 'author', 'source', 'text']

Or you can also create an in-line object - from within your any programs with more explicit declaration:

  pobject Article => {
    columns => ['id', 'title', 'date', 'author', 'source', 'text']

Effect of the above two examples is identical - Article object. By default, Class::PObject will fall back to 'file' driver if you do not specify any drivers. So the above Article object could also be redefined more explicitly as:

  pobject Article => {
    columns => \@columns,
    driver => 'file'

The above examples are creating temporary objects. These are the ones stored in your system's temporary location. So if you want more 'permanent' objects, you should also declare its datasource:

  pobject Article => {
    columns => \@columns,
    datasource => 'data/articles'

Now, the above article object will store its objects into data/articles folder.

Class declarations are tightly dependant to the type of driver being used, so we'll leave the rest of the declaration to specific drivers. In this document, we'll concentrate more on the user interface of the Class::PObject - something not dependant on the driver.


After you define an object, as described above, now you can create instances of those objects. Objects are created with new() - constructor method. To create an instance of the above Article object, we do:

  $article = new Article();

The above syntax will create an empty Article object. We can now fill 'columns' of this object one by one:

  $article->title("Persistent Objects with Class::PObject");
  $article->date("Sunday, June 08, 2003"),
  $article->author("Sherzod B. Ruzmetov");
  $article->source("lost+found (");

Another way of filling in objects, is by passing column values to the constructor - new():

  $article = new Article(title  =>  "Persistent Objects with Class::PObject",
                         date   =>  "Sunday, June 08, 2003",
                         author =>  "Sherzod Ruzmetov",
                         source =>  "lost+found ("; );
  $article->text("CONTENTS OF THE ARTICLE GO HERE");

Notice, above example is initializing all the properties of the object except for 'text' in the constructor, and initializing 'text' separately. You can use any combination, as long as you are satisfied.


Usually, when you create the objects and fill them with data, they are in-memory data structures, and not attached to any disk device. It's when you call save() method of those objects when they become so. To store the above article into disk:


save() method returns newly created object id on success, undef on failure. So you may want to check its return value to see if it succeeded:

  my $new_id = $article->save() or die "couldn't store the article";

Note: we'll talk more about handling exceptions in later sections.


No point of storing stuff if you can't retrieve them when you want. Class::PObject objects support load() method which allows you do that. You can retrieve objects in many ways. The easiest, and the most efficient way of loading an object from the disk is by its id:

  my $article = Article->load(1251);

the above code is retrieving an article with id 1251. You can now either display the article on your web page:

  printf("<h1>%s</h1>",  $article->title);
  printf("<div>By %s</div>", $article->author);
  printf("<div>Posted on %s</div>", $article->date);
  printf("<p>%s</p>", $article->text);

or you can make some changes, say, change its title and save it back:

  $article->title("Persistent Objects in Perl made easy with Class::PObject");

Other ways of loading objects can be by passing column values, in which case the object will retrieve all the objects from the database matching your search criteria:

  my @articles = Article->load({author=>"Sherzod Ruzmetov"});

The above code will retrieve all the articles from the database written by ``Sherzod Ruzmetov''. You can specify more criteria to narrow your search down:

  my @articles = Article->load({author=>"Sherzod Ruzmetov", source=>"lost+found"});

The above will retrieve all the articles written by ``Sherzod Ruzmetov'' and with source ``lost+found''. We can of course, pass no arguments to load(), in which case all the objects of the same type will be returned.

Elements of returned @array are instances of Article objects. We can generate the list of all the articles with the following syntax:

  my @articles = Article->load();
  for my $article ( @articles ) {
    printf("[%02d] - %s - %s - %s\n", $article->id, $article->title, $article->author, $article->date);

load() also supports second set of arguments used to do post-result filtering. Using these sets you can sort the results by any column, retrieve first n number of results, or do incremental retrievals. For example, to retrieve first 10 articles with the highest rating (assuming our Article object supports 'rating' property):

  my @favorites = Article->load(undef, {sort=>'rating', direction=>'desc', limit=>10});

The above code is applying descending ordering on rating column, and limiting the search for first 10 objects. We could also do incremental retrievals. This method is best suited for web applications, where you can present ``previous/next'' navigation links and limit each listing to some number:

  my @articles = Article->load(undef, {offset=>10, limit=>10});

Above code retrieves records 10 through 20.


Objects created by Class::PObject support method called ``remove()'' and ``remove_all()''. ``remove()'' is an object method, can used only to remove one object at a time. ``remove_all()'' removes all the objects of the same type, thus a little more scarier.

Note, that all the objects can still be removed with ``remove()'' method, without any need for more explicit ``remove_all()''. So why two methods? On some drivers, removing all the objects at once is more efficient than removing objects one by one. Perfect example is 'mysql' driver.

To remove an article with id 1201, we first need to create the object of that article by loading it:

  # we first need to load the article:
  my $article = Article->load(1201);

remove() will return any true value indicating success, undef on failure.

  $article->remove() or die "couldn't remove the article";

remove_all(), on the other hand, is a static class method:



If you are defining the object in its own class file, you can extend the class with custom methods. For example, assume you have a User object, which needs to be authenticated before they can access certain parts of the web site. It may be a good idea to add ``authenticate()'' method into your User class, which either returns the User object if he/she is logged in properly, or returns undef.

  package User;
  pobject {
    columns     => ['id', 'login', 'psswd', 'email'],
    datasource  => 'data/users'
  sub authenticate {
    my $class = shift;
    my ($cgi, $session) = @_;
    # if the user is already logged in, return the object:
    if ( $session->param('_logged_in') ) {
      return $class->load({id=>$session->param('_logged_in')});
    # if we come this far, we'll try to initialize the object with CGI parameters:
    my $login     = $cgi->param('login')    or return undef;
    my $password  = $cgi->param('password') or return undef;
    # if we come this far, both 'login' and 'password' fields were submitted in the form:
    my $user = $class->load({login=>$login, psswd=>$password});
    # if the user could be loadded, we set the session parameter to his/her id
    if ( defined $user ) {
      $session->param('_logged_in', $user->id);
    return $user;

Now, we can check if the user is logged into our web site with the following code:

  use User;
  my $user = User->authenticate($cgi, $session);
  unless ( defined $user ) {
    die "You need to login to the website before you can access this page!";
  printf("<h2>Hello %s</h2>", $user->login);

Notice, we're passing CGI and CGI::Session objects to authenticate. You can do it differently depending on the tools you're using.


Objects created with Class::PObject tries never to die(), and lets the programer to decide what to do on failure, (unless of course, you insult it with wrong syntax).

Methods that may fail are the one to do with disk access, namely, save(), load(), remove() and remove_all(). So it's advised that you check these methods' return values before you assume any success. If an error occurs, the above methods will return undef. More verbose error message will be accessible through error() method. In addition, save() method should always return the object id, either newly created, or updated.

  my $new_id = $article->save();
  unless ( defined $new_id ) {
    die "couldn't save the article: " . $article->error();
  Article->remove_all() or die "couldn't remove objects:" . Article->error;


Following are the lists of features and/or fixes that need to be applied before considering the library ready for production environment. The list is not exhaustive. Feel free to add your suggestions.


The library should be tested more.


Currently driver specifications are not very well documented. Need to spend more time to come up with more intuitive and comprehensive specification.


load() will not be all we need until it supports at least simple joins. Something similar to the following may do:

  @articles = Article->load(join => ['ObjectName', \%terms, \%args]);

I believe it's something to be supported by object drivers, that's where it can be performed more efficiently.


Class::PObjects try to cache the driver object for more extended periods than current object's scope permits them to. So a ``global'' DESTROY should be applied to prevent memory leaks or other unfavorable consequences, especially under persistent environments, such as mod_perl or GUI environments.

At this point, I don't have how to implement it the best way.


the Class::PObject::Driver manpage


coming soon...


the Class::PObject manpage, the Class::PObject::Driver::mysql manpage, the Class::PObject::Driver::file manpage


Sherzod Ruzmetov <>