Peter Scott wrote:
>
> Um, this is a tiny little diversion here prompted by something that
> came up on perl-beginners, of all places... it's not possible in
> perl 5 to make a reference to an array or hash slice without doing
> some copying.
>
Hey, this is *Perl*! Of course it's possible...
-----------cut-----------cut-----------cut-----------cut-----------cut----------
@A = (1..10); # array
@I = (3..5); # indices of desired slice
$ref =
sub{my%k;@k{@{+pop}}=\(@_);splice@_,$_,!$k{$_}for reverse 0..@_;\@_}->(@A,\@I);
print "@A\n";
print "@A[@I]\n";
print "@$ref\n";
$ref->[1] = 99;
print "@A\n";
print "@A[@I]\n";
print "@$ref\n";
-----------cut-----------cut-----------cut-----------cut-----------cut----------
;-)
>
> It would be nice if perl 6 made that possible.
>
I guess that would be:
$ref = sub{my%k;%k{+pop}=\(@_);splice@_,$_,!%k{$_}for @_..0:-1;@_}.(@A,\@I);
>
> Maybe it already does and I haven't grokked that from the exegeses yet.
>
I'm pretty sure Exegesis II doesn't mention *that* particular technique ;-)
Damian