I believe I have addressed your concerns with this:
[%# Run this through tpage - from Template %]
[% debug = 0 %]
[% numbers = [ 1, 2, 3, 4, 5, 6 ] -%]
[% end = '\)' -%]
[% start = '\w+\(' -%]
my %functions = ();
[% FOR level = numbers -%]
$functions{ [% level %] } = sub {
my $string = shift;
[% IF debug %]my $warn = "running [% level %] on '$string'";[% END %
+]
$string =~ s/[% end %]{[% level %]}$//;
$string =~ s/^([% start %]){[% level %]}//;
[% IF debug %]warn "$warn to produce '$string'\n\n";[% END %]
return $string;
};
[% END -%]
$functions{-1} = sub {
my $string = shift;
$string =~ m/\(([^()]+)\)+/;
[% IF debug %]warn "running -1 on $string to produce '$1'\n";[% END
+%]
return $1;
};
sub get_proparg_evdb {
my $string = shift;
my $level = shift || return $string;
return &{$functions{ $level }}( $string )
|| die 'Add universal function here';
}
I also wrote the following test code which should check that the function is behaving as it should:
# Test the function against another with known good results.
for (@data) {
my @args = split;
my @copy = @args;
my $A = get_proparg( @args );
my $B = get_proparg_evdb( @args );
unless ( $A eq $B ) {
warn "function wrong for '$args[1]'\n";
warn "A is '$A'\nB is '$B'\n\n";
}
warn "array messed up\n" unless join(' ',@copy) eq join(' ',@args);
}
I took into account the uselessness of \o (thank you diotalevi) and also removed the case == -1 check by creating a function for it. Interestingly the results are now even better:
Rate m-elegant ori ori++ evdb
m-elegant 1797/s -- -6% -11% -36%
ori 1914/s 7% -- -5% -32%
ori++ 2016/s 12% 5% -- -28%
evdb 2818/s 57% 47% 40% --
If I have misunderstood your critique please let me know. As you can see you can set debug = 1 in the template stuff to enable messages during the run that shows that all cases are being used.
--tidiness is the memory loss of environmental mnemonics |