use MooX::Pression prefix => 'MyApp'; class ::Service { has data (is => ro, type => Str, required => true); has template (is => rw, type => Str, builder => true); method populate () { (my $tmpl = $self->template) =~ s/\[data\]/$self->data/emsi; $self->template($tmpl); } method new_by_name (Str $name, Str $data) { $name = uc $name; my $child = "$class\::$name"; $child->new(data => $data); } class +XML { method _build_template () { '[data]' } } class +JSON { method _build_template () { '{"data": [data]}' } } } 1;