Is there a simpler or better (=>easier to maintain) way to use Perl and Moose
to instantiate classes based on incoming data?
The following code is a stripped down sample from a project I'm working on.
package FooBar;
use Moose;
has 'SUBCLASS' =>('isa'=>'Str',required=>'1',is=>'ro');
has 'MSG' =>('isa'=>'Str',required=>'1',is=>'ro');
sub BUILD {
my $self = shift;
my ($a)=@_;
bless($self,$a->{SUBCLASS})
}
sub Hi {
my $self=shift;
print "Hi, I'm a " . ref($self) ." and I say [". $self->MSG()."]\n";
}
package Foo;
use Moose;
extends ("FooBar");
package Bar;
use Moose;
extends ("FooBar");
package main;
use strict;
use warnings;
for my $line (<DATA>) {
my ($case,$msg)=split(/[\n\r,]\s*/,$line);
FooBar->new(SUBCLASS=>$case,MSG=>$msg)->Hi();
}
__DATA__
Foo, First Case
Bar, Second Case
EDIT: It just struck me that this is pretty much what happens when you call the DBI. Depending on the parameters you pass, it will use entirely different code while maintaining a (mostly) consistent interface