Dynamic object name question

Andy Armstrong andy at hexten.net
Thu Jun 30 21:28:15 BST 2011


On 30 Jun 2011, at 21:18, Dan Brook wrote:
> You can just dispatch on a string e.g
> 
>  if(exists $possible_classes{$t}) {
>    my $class = ucfirst $t;
>    $class->new(@args);
>  }
> 
> Filling in %possible_classes is left as an exercise to the reader.

If you're going to have a validation hash you might as well put the class names in that - then most of them can be regularly derived from values that $t can take but it allows for exceptions.

my %possible_classes = (
  ( map { $_ => 'Foo::' . ucfirst lc $_ } qw( thing BOO Boggle ) ),
  ( '123' => 'Foo::OneTwoThree', '$$$' => 'Foo::Cash' )
);




More information about the london.pm mailing list