Let's say I have the arrays @foo = 1 .. 5; and @bar = @foo[0..2]; I want to be able to splice(@bar, 1, 0, 6); and have is_deeply(\@foo, [1,6,2,3,4,5]); pass.

Basically, I want to have an array act as an alias to a sub-array of another array. If it helps, it can be a contiguous slice. I understand that slicing creates a completely new list, unrelated to the old one, so slicing as implemented isn't the answer.

I also know I can use tie for this, but I really want to avoid tie, if at all possible. The application1 of this is going into the refactoring of a heavily-used CPAN module, so performance is more important that a slight memory gain.

Is there a way to alias like I'm talking about? Or is this a Perl6-ism that isn't possible in Perl5? I would think that XS could provide a solution, but I don't know enough about XS to even make a stab at the feasibility of my crackpipe-dreams.

  1. Basically, I have an object which is a hashref that also contains an arrayref. I figured that I could make that into an arrayref as follows:
    • Index 0 is the size of the arrayref. (This would be maintained by hand.)
    • Index 1..M is the arrayref
    • Index M+1..N are the other attributes. They are accessed as so
      sub foo { my $self = shift; return $self->[ $self->[0] + $foo_offset ]; }
    • New attributes from subclasses can be added onto the end.

My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

In reply to (Expert) Splicing a slice by dragonchild

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.