in reply to Re^3: Local $$_?
in thread Local $$_?

Thank you LanX! You're correct in assuming this will be used in a loop. An FCGI Loop infact. Some older scripts were not designed with lexicallity in mind and simply make use of global variables if and when they're values are needed. Localizing these at the beginning of the FCGI Loop allowed me to run the given program under FCGI without re-writing it. Early indications show an improvement of over 500%. Although it is not a perfect solution - it seems to be working.

Replies are listed 'Best First'.
Re^5: Local $$_?
by LanX (Saint) on Mar 15, 2010 at 12:54 UTC
    Yeah I was expecting something like reusing old code.

    Glad if I was able to help! :)

    Cheers Rolf

    UPDATE: But you should do thorough testing to be sure about the side-effects! Be aware: In many cases testing and fixing resulting bugs aren't cheaper than rewriting ...

    Using lexical variables are almost always better!

Re^5: Local $$_?
by Anonymous Monk on Mar 15, 2010 at 13:03 UTC

    All you really needed was

    print "local \$$_ = \$$_;\n" for @listofvarnames;
    then you copy/paste
    local $blah = $blah; ...

    Although it is not a perfect solution - it seems to be working.

    The use of global variables is red flag. Your statement that "it seems to be working" also indicates that it doesn't come with a test suite, another red flag. I think now is a time to consider/plan to refactor this program.

Re^5: Local $$_?
by LanX (Saint) on Mar 15, 2010 at 13:28 UTC
    > You're correct in assuming this will be used in a loop.

    Maybe a misunderstanding, in the OP you were trying to localize package vars in a loop, this can't be done because of the loop scope.

    But of course you are free to initialize the vars in a loop or a list assign _after_ you localized them.

    Cheers Rolf