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
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.
|
|---|
| 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 | |
|
Re^4: Getting for() to accept a tied array in one statement
by perlancar (Hermit) on Apr 16, 2019 at 14:02 UTC |