in reply to Re^2: grep { } positional number of element
in thread grep { } positional number of element

I think there is, or, at least, *was*, good reason why each was erased from our concsiousness. I do not know what the state of these bugs is today. I was definetely bitten by its non-reentrancy /sic/ several times in nested each loops.

Replies are listed 'Best First'.
Re^4: grep { } positional number of element
by Bod (Parson) on Oct 25, 2022 at 16:35 UTC
    good reason why each was erased from our concsiousness [sic]

    Thanks for bringing that to our attention 🙂

    It seems that each is OK provided we know for sure that the array or hash that is being iterated is not being modified during the iteration. Much of the time we can be sure of that I think. But, of course, it is easy to overlook that the data could be modified, particularly if each is used in a module.

      > Much of the time we can be sure of that I think.

      Only if you don't call any other routines from any other module while using each.

      I seem to remember that one of the dumper modules messed with the each counter.

      Similarly if you export your sub and the data structure is passed as variable. Than the importing code could get into trouble using each.

      Otherwise you need to copy the hash/array beforehand.

      (all of this would be much easier if there was a way to localize the internal counter)

      update

      in short, each X is only safe if X is never passed around.

      Cheers Rolf
      (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
      Wikisyntax for the Monastery