in reply to Re^3: Getting for() to accept a tied array in one statement
in thread Getting for() to accept a tied array in one statement
Ah sorry, I stand corrected. I was reading your code as: for( do { tie my @x, "MyClass", "first", "second"; @x } ) {
So indeed: for( @{ tie my @x, "MyClass", "first", "second"; \@x } ) {
will make for() iterate over a tied array. In fact,
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 { return scalar @data } package main; sub wrapper { tie my @x, "MyClass", @_; \@x; } for( @{wrapper("first", "second")} ) { print "In loop = "; print "$_\n"; }
also works. So it's quite close to what I want, yay. Any improvement is welcome :-)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Getting for() to accept a tied array in one statement
by hdb (Monsignor) on Apr 16, 2019 at 14:18 UTC | |
by perlancar (Hermit) on Apr 16, 2019 at 14:23 UTC | |
by ikegami (Patriarch) on Apr 19, 2019 at 08:45 UTC | |
by Veltro (Hermit) on Apr 19, 2019 at 11:35 UTC | |
by ikegami (Patriarch) on Apr 19, 2019 at 17:59 UTC | |
| |
by perlancar (Hermit) on Apr 22, 2019 at 07:45 UTC | |
by ikegami (Patriarch) on Apr 22, 2019 at 21:56 UTC | |
|