여러 템플릿 기반 스크립트는 (거의) 올바른 대답이 아닙니다.
구성 파일의 데이터 구조를 사용하고 구조를 제어하여 필요한 동작을 얻습니다.
대신 코드를 생성하기 위해 템플릿을 사용하는 것처럼 : 생성하여 (즉, '당신의 기능을 카레'입니다)
# Get this from a config file. YAML perhaps
my $cfg = {
foo => [ \&blarg, \&feemb ],
bar => [ \&crag, \&forg ],
baz => [ \&chomb, \&veezle ],
};
sub calculate_product {
my $cfg = shift;
my $type = shift;
my $thing = shift;
my @subs_to_call = @{ $cfg->{$type} || [] };
my $result = {shift @subs_to_call}->($thing};
$result *= $_->($thing) for @subs_to_call;
return $result;
}
# Call like so:
my $foo_prod = calculate_product($cfg, 'foo', 15);
당신은 서브 루틴에 설정 정보를 결합 할 수 있습니다
sub foo {
my $thing = shift;
return blarg($thing) * feemb($thing);
}
sub bar {
my $thing = shift;
return crag($thing) * forg($thing);
}
sub baz {
my $thing = shift;
return chomb($thing) * veezle($thing);
}
은이 작업을 수행 구성 정보로 폐쇄 :
# Get this from a config file. YAML perhaps
my $cfg = {
foo => [ \&blarg, \&feemb ],
bar => [ \&crag, \&forg ],
baz => [ \&chomb, \&veezle ],
};
my %calculate_product;
for my $product (keys %$cfg) {
my @stored_subs_to_call = @{$cfg->{$product} || [] };
$calculate_product{$prod} = sub {
my $thing = shift;
my @subs_to_call = @stored_subs_to_call;
my $result = {shift @subs_to_call}->($thing};
$result *= $_->($thing) for @subs_to_call;
return $result;
}
}
# Call generated code like so:
my $foo_prod = $calculate_product{foo}->(15);