in reply to Interesting SEEK behavior on tied filehandle

Many tie methods force their arguments into more regular forms. You can't get anything but non-negative integers for indices into tied arrays ($x[-1] calls $obj->FETCH( $obj->FETCHSIZE() -1 ), for example). You can't use nor return a list where you would usually have a scalar. And so on...

An exception appears to be that you can get more than just the string value for keys passed to tied hashes (and I suspect this was not always the case).

- tye        

Replies are listed 'Best First'.
Re^2: Interesting SEEK behavior on tied filehandle ($x[-@x-1])
by ikegami (Patriarch) on Aug 02, 2006 at 20:29 UTC

    I thought I might be able to pass a negative number to FETCH if the index was negative and bigger than the number of elements.

    Turns out FETCH is not even called in that situation, even though FETCH *is* called for indexes greater than the highest existing index.

    use Tie::Array qw( ); our @ISA = 'Tie::StdArray'; sub FETCH { my ($self, $idx) = @_; warn("Fetching index $idx\n"); return $self->SUPER::FETCH($idx); } my @a; tie @a, __PACKAGE__; $#a = 4; my $s; $s = $a[-@a+1]; # Fetching index 1 $s = $a[-@a]; # Fetching index 0 $s = $a[-@a-1]; # FETCH not called! $s = $a[@a]; # Fetching index 5

    Perl 5.6.1 & 5.8.6