in reply to Can a class constructor create an instance of a derived class and return it? Or, can objects transmute into other objects?

If i understand you correctly, it's the "Factory method" pattern case. I'm using it for storage abstraction.

package Storage::Factory; { use Storage::SQLite; use Storage::MySQL; sub new { my $class = shift; my $opt = {@_}; return Storage::SQLite->new(file => $opt->{dsn}) if $opt->{typ +e} eq 'sqlite'; return Storage::MySQL->new(dsn => $opt->{dsn}) if $opt->{type} + eq 'mysql'; } } 1; use Storage::Factory; my $storage = Storage::Factory->new(%{ $self->plugin('Config')->{db} } +)
  • Comment on Re: Can a class constructor create an instance of a derived class and return it? Or, can objects transmute into other objects?
  • Download Code