Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Getting for() to accept a tied array in one statement

by hdb (Monsignor)
on Apr 16, 2019 at 12:15 UTC ( #1232647=note: print w/replies, xml ) Need Help??


in reply to Getting for() to accept a tied array in one statement

Never used tie before, so this is a good learning experience, even though like LanX I do not know either what this is good for. In any case, I think, this works as required:

for( @{ tie my @x, "My::Class", "first", "second"; \@x } ) { print "$_\n"; }

Replies are listed 'Best First'.
Re^2: Getting for() to accept a tied array in one statement
by perlancar (Friar) on Apr 16, 2019 at 13:05 UTC
    Your code which wraps tie in @{ ... } does not work because all elements are FETCH-ed first before the loop block code is executed. I.e. in the above case, FETCH(0) and FETCH(1) are called before "first" and "second" are print-ed by print(). The @{ ... } basically turns the tied array into list of values. I need to iterate over the tied array so the code in the loop block (in this case, print()) is executed along with FETCH/FETCHSIZE.

      I disagree. Here is the full code and FETCH is only called once for each element within the body of the loop:

      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 { print "<FETCHSIZE> "; return scalar @data } package main; for( @{ tie my @x, "MyClass", "first", "second"; \@x } ) { print "In loop = "; print "$_\n"; }

      Output:

      In loop = FETCH: first In loop = FETCH: second

        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 :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1232647]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2021-04-23 08:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?