[CDBI] how to generate a virtual column object from more that one real columns

Michael G Schwern schwern at pobox.com
Sat Mar 10 22:24:34 GMT 2007

Mike Demoulin wrote:
> Here's what I want to do. I have a table (person) that has, among other
> columns, fname, mname, and lname. I have a Class::DBI subclass (Person)
> to handle that table. I have a class (Name) to encapsulate those 3
> values into one object. Instead of constructing the Name form Person
> after each query, I would like Person to be able to treate name as a
> column, and to return a Name.

How about this?

sub name {
    my $self = shift;

    my $name;
    if( @_ ) {
        $name = Name->new_from_name(shift);
    elsif( !defined $self->{_name} ) {
        $name = Name->new_from_parts(
            first  => $self->fname,
            middle => $self->mname,
            last   => $self->lname

    return $self->{_name} ||= $name;

You could probably also do something with inflate/deflate.

More information about the ClassDBI mailing list