in reply to A 'print' at one spot changes a value at another

At compile time, perl doesn't know that the eval string will close over $sep, so it doesn't. If you changed it to a normal closure, it ought to work.

Arguably this is or is not a bug. (If you think it is a bug, please post an algorithm that reliably fixes the bug. Good luck.)

Replies are listed 'Best First'.
Re^2: A 'print' at one spot changes a value at another
by pg (Canon) on Aug 15, 2005 at 05:17 UTC

    A even better advise is to avoid closure entirely.

Re^2: A 'print' at one spot changes a value at another
by ralphmerridew (Acolyte) on Aug 15, 2005 at 12:22 UTC
    In my actual program, the code to the function is generated at runtime, so I can't use a normal closure. Changing the respective 'my' to 'our' seems to have fixed the code, but I'm really bothered by the inconsistency that it works when I have that print statement.

    (Maybe the print statement prevents it from making some optimization which isn't valid in the situation?)

      The print statment lets perl know to close on the $sep variable. Without the print, perl doesn't know you want to access $sep because it doesn't look inside the eval. Changing to our makes $sep global so that it will always be available, not just when perl sees a function is going to use it.


      ___________
      Eric Hodges