in reply to Re^3: index wrapping in tied FETCH and EXISTS
in thread index wrapping in tied FETCH and EXISTS

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 ... }

Replies are listed 'Best First'.
Re^5: index wrapping in tied FETCH and EXISTS
by cmac (Monk) on Feb 05, 2009 at 06:15 UTC
    Thank you, mr. i!

    In my case this technique involved making the AUTOLOAD routine tag the array routine names (to which tie calls go) with "_nowrap", and modifying the XS linkage routines like this:
    SV * abc_array_fetch (array, index) abc_array *array IV index ALIAS: abca_array_fetch=1 abc_array_fetch_nowrap=2 abca_array_fetch_nowrap=3 CODE: if (index < 0 && !(ix & 2)) index += array->entries; RETVAL = abc_array_fetch (array, index, ix&1);
    The best part is, it seems to work.

    Thanks again,
    cmac
    www.animalhead.com