On Thu, Jul 18, 2002 at 01:21:36PM -0400, darren chamberlain wrote:
> Nope, it was I who was confused. Ok, so *now* how does this look:
Not bad, just got a couple of minor tweaks:
'splice' => sub {
my ($list, $offset, $length, @replace) = @_;
if (@replace) {
# @replace can contain a list of multiple replace items, or
# be a single reference to a list
@replace = @{ $replace[0] }
if @replace == 1 && ref $replace[0] eq 'ARRAY';
return [ splice @$list, $offset, $length, @replace ];
}
elsif (defined $length) {
return [ splice @$list, $offset, $length ];
}
elsif (defined $offset) {
return [ splice @$list, $offset ];
}
else {
return [ splice(@$list) ];
}
},
Most importantly, we should return a reference to a list of the items
spliced out. This keeps thing as-per-perl, and allows you to do things
like this:
[% my_list.splice(-3, 3).join(', ') %]
When you don't want to see the result, you can use CALL:
[% CALL my_list.splice(...) %]
Also, I think we should allow the replacement list to be specified as a
reference to a list:
[% my_list.splice(3, 4, my_other) %]
as well as a list of items:
[% my_list.splice(3, 4, a, b, c) %]
That could be ambiguous if you wanted to splice a reference to a list (not
the contents) into another list, but that strikes me as something you
should probably be doing in Perl. :-)
And if you really do need to do that, you can always be explicit:
[% my_list_of_list_refs.splice(3, 4, [ new_list_ref ]) %]
Ok, so *now* how does this look? :-)
A