use constant ATTACK_TYPES => [ { type => 'light', damage => 1, speed => 5, }, { type => 'fast', damage => 1, speed => 7, }, { type => 'heavy', damage => 2, speed => 4, }, ]; foreach my $attack ( @{ATTACK_TYPES()} ) { my $name = "new_$attack->{type}"; { no strict 'refs'; *{$name} = sub { return shift->new(%$attack) }; } } #### use IO::All; use JSON 'decode_json'; use constant ATTACK_PARAMS => [ qw/ type damage speed / ]; my $attack_types = decode_json( io('attacks.json')->slurp ); foreach my $attack ( @$attack_types ) { exists $attack->{$_} or die "Malformed attack type: $_\n" for @{ATTACK_PARAMS()}; my $name = "new_$attack->{type}"; { no strict 'refs'; *{$name} = sub { return shift->new(@{$attack}{ATTACK_PARAMS()}) }; } }