Dynamic object name question

Andy Armstrong andy at hexten.net
Thu Jun 30 21:10:40 BST 2011


On 30 Jun 2011, at 20:54, Ian Knopke wrote:

> Hi everyone,
> 
> I have a whole list of subobjects of the form:
> 
> foreach my $t(@types){
> 
>    if ($t eq 'first'){
>        Top::First->new(@args);
>    } elsif ($t eq 'second') {
>        Top::Second->new(@args);
>    } elsif
>     .... (add 20 more of these here)
>    };
> }
> 
> Since the arguments coming in match the object names (except for case)
> there must be away to collapse this into something saner, but I don't
> know how. Does anyone have any advice?

die "Smelly class name" unless $t =~ /^[a-z_][a-z_0-9]*$/;
my $cl = 'Top::' . ucfirst lc $t;
$cl->new(@args);



More information about the london.pm mailing list