Here's another tied variable solution. I didn't implement most of the tied array methods; it's ill-defined what push, pop, etc. should do in this case.

All told, I think I prefer your solution after blokhead's suggestions.

package Array::AliasRange; sub TIEARRAY { my ($class, $array, $offset, $length) = @_; bless { array => $array, offset => $offset, length => $length, }, $_[0]; } sub _real_index { my ($self, $index) = @_; if ($index >= 0) { return undef if ($index + $self->{offset}) >= ($self->{offset} + $ +self->{length}); return $index + $self->{offset}; } else { return undef if (scalar @{$self->{array}} - ($index + $self->{offs +et})) < 0; return scalar @{$self->{array}} - ($index + $self->{offset}); } } sub FETCH { my ($self, $index) = @_; my $real_index = _real_index($self, $index); return defined $real_index ? $self->{array}[$real_index] : undef; } sub STORE { my ($self, $index, $newval) = @_; my $real_index = _real_index($self, $index); return defined $real_index ? ($self->{array}[$real_index] = $newval) + : undef; } sub FETCHSIZE { my $self = shift; return $self->{length}; } package main; sub by_groups_of { my ($by, $arrayref) = @_; return sub { () } if ! $by || $by =~ /\D/ || ! @$arrayref; my $offset = 0; return sub { return () if $offset >= scalar @$arrayref; my @new_array; my $length = ($offset + $by >= scalar @$arrayref) ? (scalar @$arra +yref - $offset) : $by; tie @new_array, "Array::AliasRange", $arrayref, $offset, $length; $offset += $by; return \@new_array; } } my @array = 0 .. 10; my $next = by_groups_of( 3, \@array ); while ( my $group = $next->() ) { $group->[1] = 'foo'; } print join ' ', @array, "\n";

In reply to Re: How to make references more like aliases? by Zed_Lopez
in thread How to make references more like aliases? by Limbic~Region

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.