I think it would be just as easy to use references to the original items instead. Creating the "view" is easy (put a backslash before the slice), and it doesn't rely on lvalue subs, which are still marked as experimental in perlsub.

use Data::Dumper; use Test::More 'no_plan'; my @dna = qw( A T T G C ); my @view = \@dna[1,2]; is( ${$view[0]}, $dna[1], '${$view[0]} is $dna[1]' ); ${$view[0]} = 'x'; is( ${$view[0]}, $dna[1], 'changing ${$view[0]} changes $dna[1]' ); print Dumper \@view; __END__ ok 1 - ${$view[0]} is $dna[1] ok 2 - changing ${$view[0]} changes $dna[1] $VAR1 = [ \'x', \'T' ]; 1..2

The syntax for accessing the view vs. the original array is clumsy, but that's the same as if you were using lvalue subs. If you change the original array to use references also (making them equally clumsy), the views become transparent.

my @dna = \qw( A T T G C ); my @view = @dna[1,2]; is( ${$view[0]}, ${$dna[1]}, '${$view[0]} is ${$dna[1]}' ); ${$view[0]} = 'x'; is( ${$view[0]}, ${$dna[1]}, 'changing ${$view[0]} changes ${$dna[1]}' );

I can't say I recommend that in general, but it might work well for you, if that's your goal.


In reply to Re: a referencable array slice (so assigning to it modifes the original) by kyle
in thread a referencable array slice (so assigning to it modifes the original) by muenalan@cpan.org

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.