* John Douglas Porter <[EMAIL PROTECTED]> [2006-09-08 15:40]:
> If we're looking for ways to do it differently, possibly better:
>
> my %copy = %$tmpls;
> $_ = ref $_ ? \"$$_" : \"$_" for values %copy;
> @{$self->{templates}}{ keys %copy } = values %copy;
That was one of the deleted attempts. Hmm, let me refactor…
my %copy = %$tmpls;
$_ = \( ref $_ eq 'SCALAR' ? "$$_" : "$_" ) for values %copy;
@{$self->{templates}}{ keys %copy } = values %copy;
Hmm, rewriting it that way makes it more amenable to pulling out.
sub flatten_copy {
local $_ = shift;
ref $_ eq 'SCALAR' ? "$$_" : "$_";
}
my %copy = %$tmpls;
$_ = \( flatten_copy $_ ) for values %copy;
@{$self->{templates}}{ keys %copy } = values %copy;
I like that. My previous attempts at encapsulating that
expression felt dirty, because I didn’t notice that taking the
reference could be factored out. This separation of concerns, in
contrast, feels right.
Hmm, `values %copy` is there twice… Not really necessary with the
cleaned up copy expression, I think. But nesting derefs deeply is
always hard to read in Perl, so…
sub flatten_copy {
local $_ = shift;
ref $_ eq 'SCALAR' ? "$$_" : "$_";
}
my $t = $self->{templates};
@{$t}{ keys %$tmpls } = map \( flatten_copy $_ ), values %$tmpls;
I think I like that.
Regards,
--
Aristotle Pagaltzis // <http://plasmasturm.org/>