That's not how I understand the output. It seems FETCHSIZE is only called once, but each element is fetched right before the iteration:
Output:#! /usr/bin/perl use warnings; use strict; use feature qw{ say }; { package My; use Tie::Array; use parent -norequire => 'Tie::StdArray'; sub TIEARRAY { warn "TIE: @_\n"; my $class = shift; bless [@_], $class } sub FETCHSIZE { warn "SIZE: @_\n"; return scalar @{ $_[0] } } sub FETCH { warn "FETCH: @_\n"; my ($ar, $idx) = @_; my $e = $ar->[$idx]; return ++$e } } for my $e (do { tie my @ar, 'My', qw( a b c ); @ar } ) { say "MAIN: $e"; }
TIE: My a b c SIZE: My=ARRAY(0x21eff40) FETCH: My=ARRAY(0x21eff40) 0 MAIN: b FETCH: My=ARRAY(0x21eff40) 1 MAIN: c FETCH: My=ARRAY(0x21eff40) 2 MAIN: d
for my $e (@ar), on the other hand, calls FETCHSIZE before each FETCH.
In reply to Re^3: Getting for() to accept a tied array in one statement
by choroba
in thread Getting for() to accept a tied array in one statement
by perlancar
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |