Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

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

by hdb (Monsignor)
on Apr 17, 2019 at 09:18 UTC ( [id://1232708]=note: print w/replies, xml ) Need Help??


in reply to Re^5: Getting for() to accept a tied array in one statement
in thread Getting for() to accept a tied array in one statement

Strawberry 5.16.3 :)

Replies are listed 'Best First'.
Re^7: Getting for() to accept a tied array in one statement
by LanX (Saint) on Apr 18, 2019 at 11:13 UTC
    I couldn't find any mention in the perldelta and Perldoc.org doesn't list 16.3.

    For loops used to have the limitation to flatten the iterated structure to a list, which resulted sometimes in unfortunate memory requirements.

    IIRC this was partially fixed by parsing if it's an @array or a range-operator, and acting like an iterator (ie for (reverse 1..1e18) remained problematic, because reverse was not detected)

    I'd say this is the essential hook allowing your trick of iterating an "injected" tied array.

    Did I miss something?

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      I have to admit that I am not a specialist on the Perl internals (more precisely I do not know anything about it), so my guess is the following:

      If any element of LIST is an lvalue, you can modify it by modifying VAR inside the loop. Conversely, if any element of LIST is NOT an lvalue, any attempt to modify that element will fail. In other words, the foreach loop index variable is an implicit alias for each item in the list that you're looping over.

      from perlsyn means that for anything that can be modified (i.e. an lvalue) in the loop's body, Perl must remember where it is stored. When accessing or modifying it, the tie mechanism is invoked. So this version

      tie @ary, "My::Class", "some", "el", "ems"; for (@ary) { some_code($_); }

      must have worked ever since tie exists. Using sub wrapper : lvalue {...; return @x} is only turning the result from the sub to be an lvalue (according to the docs since Perl 5.6).

      Whether or not this is what is really happening I do not know.

      Update: reversed lvalues seem to be fine as well:

      my @arr1 = (0) x 4; my $n = 0; for( reverse @arr1 ) { $_ += $n++; } print "@arr1\n"; __END__ Output 3 2 1 0
        Wait now I am confused...

        I thought the whole spectacle is about hooking into the iterator on the arrays level.

        But you are talking about the lvalue character on the elements level...

        Probably I need to start reading the whole thread again ...

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re^7: Getting for() to accept a tied array in one statement
by LanX (Saint) on Apr 17, 2019 at 11:33 UTC
    exactly! :)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-03-29 09:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found