http://qs321.pair.com?node_id=1232652


in reply to Re^2: Getting for() to accept a tied array in one statement
in thread Getting for() to accept a tied array in one statement

I disagree. Here is the full code and FETCH is only called once for each element within the body of the loop:

use strict; use warnings; package MyClass; use Tie::Array; our @ISA = ('Tie::Array'); our @data; # mandatory methods sub TIEARRAY { my $class = shift; bless \@data, $class; @data = @_ +; return \@data } sub FETCH { print "FETCH: "; my ($self, $index ) = @_; return $dat +a[$index] } sub FETCHSIZE { print "<FETCHSIZE> "; return scalar @data } package main; for( @{ tie my @x, "MyClass", "first", "second"; \@x } ) { print "In loop = "; print "$_\n"; }

Output:

In loop = FETCH: first In loop = FETCH: second