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

Michael G Schwern <[email protected]>
Newsgroups gmane.comp.lang.perl.modules.class-dbi
Message-ID <[email protected]>
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);
        $self->fname($name->fname);
        $self->mname($name->mname);
        $self->lname($name->lname);
    }
    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.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.