in reply to index wrapping in tied FETCH and EXISTS

Oh.

If non-tied arrays work that way too, the our NEGATIVE_INDICES=1;I put in my tied array class will come out. Also the underlying direct-call methods for array handling will have to change, in how they handle a negative index.

The original note came about because I was adapting a test sequence for said direct-call methods to a tied array, and got irked when the tie structure broke a test for "outside the array" (below zero).

Sorry,
cmac

Replies are listed 'Best First'.
Re^2: index wrapping in tied FETCH and EXISTS
by ikegami (Patriarch) on Feb 04, 2009 at 16:32 UTC

    You can reach lower than the start of the array by using an index lower than -@array.

    $ perl -wle'$|=1; @a=qw(a b c); print "$_: $a[$_]" for -@a-1..$#a+1' Use of uninitialized value in concatenation (.) or string -4: -3: a -2: b -1: c 0: a 1: b 2: c Use of uninitialized value in concatenation (.) or string 3:
      To me your test shows that you CAN'T reach lower than the start of the array by using an index lower than -@array. A return value of undef is the only way that perl has to tell you that you're doing something bad. OK, not the only way, but dieing/croaking seems too strong for an out-of-range array index.

      It occurred since my previous note that I shouldn't emulate this behavior in the direct-call routines that underlie my tied arrays, because then a user of a tied array would be faced with two layers of "wrapping around".

      Unless I can figure out how to differentiate a tied call from a direct one. Since tied calls come through a little "dereference wrapper" from my AUTOLOAD routine (its PM thread ended yesterday), this should be possible, but I'm not sure it's worth the overhead.

      cmac
      www.animalhead.com

        To me your test shows that you CAN'T reach lower than the start of the array

        I'd love to see what's your definition "can" in this situation. Let's say you're right and you can't reach before the start of the array. What would be returned if you could?

        To me, "can" would be returning what's at that location. It correctly returns undef.

        Unless I can figure out how to differentiate a tied call from a direct one.

        sub FETCH { my ($self, $i) = @_; return $self->_fetch($i); } sub fetch { my ($self, $i) = @_; $i += $self->num_eles() if $i < 0; return undef if $i < 0; return $self->_fetch($i); } sub _fetch { my ($self, $i) = @_; # $i is guaranteed to be in range ... }