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

> because for() gets an ordinary (non-magical) list of values.

That's not how I understand the output. It seems FETCHSIZE is only called once, but each element is fetched right before the iteration:

#! /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"; }
Output:
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.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^4: Getting for() to accept a tied array in one statement
by ikegami (Patriarch) on Apr 19, 2019 at 08:39 UTC

    It seems FETCHSIZE is only called once, but each element is fetched right before the iteration:

    That's correct. for (@a) iterates over the indexes as if you had done for (0..$#a). This differs from for ((), @a), which retrieves the contents of @a up front.

Re^4: Getting for() to accept a tied array in one statement
by perlancar (Hermit) on Apr 16, 2019 at 14:02 UTC

    Yup, I was referring to for(), where an additional FETCHSIZE is invoked for each iteration, which is useful in the case where we want to size to be dynamic/growing.