Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Ovid (2709)

  (email not shown publicly)
AOL IM: ovidperl (Add Buddy, Send Message)

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Journal of Ovid (2709)

Monday December 30, 2002
01:47 PM

Classes as objects

[ #9677 ]

Due to some feedback received here, I have stopped my work on developing persistent objects and switched to Class::DBI. My only significant problem with Class::DBI is that method names default to the field names. I prefer that they be decoupled so that I can change field names without having to change the code in many places. To get around that, I was doing some work with base classes and symbolic references to ensure that I was setting up accessor and mutator names correctly, but this was using package variables and violating encapsulation, so I wasn't happy with that, either. Finally, after discussing the problem with a friend, the solution became clear. I turned the classes into objects and call methods on them. This has improved encapsulation and automatically meshes nicely with Class::DBI's methods of creating proper accessor and mutator names.

package Foo::SaleItem;
use base 'Foo::Class';
use strict;
use warnings;

my $map = {
    sale_id       => 'sale',
    sku           => 'sku',
    quantity      => 'quantity',
    actual_price  => 'actualPrice',
    applied_price => 'appliedPrice'

my $data = {
    id     => 'sale_item_id',
    fields => $map,

my $class = __PACKAGE__->initialize( $data );
$class->columns( All => $class->get_fields );


The great thing about that is the following line:

my $class = __PACKAGE__->initialize( $data );

That sets up all of my class data and creates the accessor and mutator names for the subclass, all without any extra work on the part of the programmer. I understand that Ruby and SmallTalk both treat classes as objects and I'm curious to see what they do with them. It saved me a lot of work.

Update: The other benefit is that class data is stored in one and only one place. That's one thing that always bugged me about Perl -- having five or six different objects, all of whom carry around copies of the class data.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.