Newsgroups: perl.perl6.language Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!nntp.perl.org Return-Path: Mailing-List: contact perl6-language-h...@perl.org; run by ezmlm Delivered-To: mailing list perl6-langu...@perl.org Delivered-To: perl6-langu...@perl.org Date: Mon, 7 Jul 2003 08:03:59 -0700 To: perl6-langu...@perl.org Subject: Re: Aliasing an array slice Message-ID: <20030707080359.A75579@megazone.bigpanda.com> Mail-Followup-To: perl6-langu...@perl.org References: <20030705180940.U1177@plum.flirble.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5i In-Reply-To: ; from fibonaci@babylonia.flatirons.org on Sat, Jul 05, 2003 at 12:57:54PM -0600 X-SMTPD: qpsmtpd/0.26, http://develooper.com/code/qpsmtpd/ X-Spam-Check-By: one.develooper.com X-Spam-Status: No, hits=-1.5 required=7.0 tests=CARRIAGE_RETURNS,DATE_IN_PAST_06_12,GAPPY_TEXT,IN_REP_TO,REFERENCES,SPAM_PHRASE_00_01,USER_AGENT,USER_AGENT_MUTT version=2.44 X-SMTPD: qpsmtpd/0.26, http://develooper.com/code/qpsmtpd/ Approved: n...@nntp.perl.org From: dsto...@dstorrs.com (David Storrs) Lines: 51 Thinking about it, I'd rather see lvalue slices become a nicer version of C. my @start = (0..5); my @a = @start; @a[1..3] = qw/ a b c d e /; print @a; # 0 a b c d e 4 5 # Similarly: @a = @start; my $r_slice = \@a[1..3]; @$r_slice = qw/ a b c d e /; print @a; # 0 a b c d e 4 5 # Note that it does NOT modify in rvalue context print reverse @$r_slice; # e d c b a print @a; # 0 a b c d e 4 5 # To modify, do this: @$r_slice = reverse @$r_slice; print @a; # 0 e d c b a 4 5 As far as what happens when you modify the array to which the slice points--treat it like any other reference. If you undef an array to which you are holding a reference, the reference is suddenly reffing a null array. If you undef an array slice to which you are holding a reference, your slice ref is now reffing undef. @a = @start; $r_slice = \@a[0..3]; print @a; # 0 1 2 3 4 5 print @$r_slice; # 0 1 2 3 shift @a; # (*) print @a; # 1 2 3 4 5 print @$r_slice; # 1 2 3 (*) There should probably be a suppressable warning emitted here, something like: "Warning: slice reference being modified" or "Warning: slice reference such-and-such included this element; ref modified" If slices DO get this functionality, it would be nice to add a method whereby we could retrieve the min/max keys (for an array) or the set of keys (for a hash) which they are currently reffing. --Dks