Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^4: Why does each() always re-evaluate its argument? (Updated x2 - experimental "for_list" )

by ikegami (Patriarch)
on Dec 07, 2023 at 01:58 UTC ( [id://11156153] : note . print w/replies, xml ) Need Help??


in reply to Re^3: Why does each() always re-evaluate its argument? (Updated x2 - experimental "for_list" )
in thread Why does each() always re-evaluate its argument?

Actually I'm surprised that copying large hashes into a list can compete with the built-in iterator of perl hashes.

1) There's no copying, and 2) it uses the built-in iterator.

When we say "returns a list", we simply mean "returns zero or more scalars". It's not a data structure. You don't copy into a list since there's no such thing. We're talking about pushing pointers onto the stack. Which is exactly what keys, each or any other approach would have to do as well. There's no extra work here, so it shouldn't be surprising that it's not any slower.

Replies are listed 'Best First'.
Re^5: Why does each() always re-evaluate its argument? ("for_list" )
by LanX (Saint) on Dec 07, 2023 at 02:49 UTC
    > 1) There's no copying,

    If that's so, then the docs should be clearer:

    > 2) it uses the built-in iterator.

    In that case, I suppose nested loops over the same hash are covered by saving and restoring the localized iterator?

    I was expecting a more intelligent solution than literally "copying to a flat list" but this also leads to another question:

    What happens if the hash is altered inside the loop, if it's not copied "before the loop starts"

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

      In that case, I suppose nested loops over the same hash are covered by saving and restoring the localized iterator?

      Nope, no need. Like you quoted, the hash's contents are fetched up front.

        > fetched up front.

        I'm confused, you just explained

        > > 1) There's no copying, 2) it uses the built-in iterator.

        Did you correct yourself or am I misunderstanding you?

        FWIW: I'm installing 5.38.2 for testing via Perlbrew and it takes hours...

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        see Wikisyntax for the Monastery