#! /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]
|