Sprad has asked for the wisdom of the Perl Monks concerning the following question:

I've got a module that has some assorted functions, and some package-global data at the top (three scalars, each declared with 'my'). This data is being a little screwy. I'm running my code in the ptk debugger, and watching the value of my package-global variables.

So the three variables get set, and that shows up fine. Then a function elsewhere in the module gets called. Immediately these variables become undefined. Nobody's redeclaring them, or even touching them. Upon returning to the original function, two of the three variables remain undefined. The third gets its original value back.

However, if I refer to the variables inside the called function (such as by printing them), they don't lose their values.

What gives?

---
A fair fight is a sign of poor planning.

Replies are listed 'Best First'.
Re: Schrodinger's Variable
by Zaxo (Archbishop) on Jan 23, 2004 at 00:28 UTC

    That sounds like unintended behavior of lexical closures, but you'll have to show the code to get a diagnosis.

    The term '"package global" is usually reserved for a true global variable in the manespace's symbol table. Try your code without my, but with our or vars.pm and see if it makes a difference.

    After Compline,
    Zaxo

      Using "our" worked. I'm a bit troubled, though, because I still don't understand what's happening, and chances are that using our is just patching up an underlying problem...

      ---
      A fair fight is a sign of poor planning.

Re: Schrodinger's Variable
by Abigail-II (Bishop) on Jan 23, 2004 at 00:51 UTC
    Try isolating your unwanted behaviour into a small piece of stand alone code. Chances are that in the process of doing so, you'll find the cause - if not, you can post it here.

    Without seeing the code, noone is going to solve your problem other then by luck ("there's a bug on line 17!").

    Abigail

      I've been trying for most of the day to do just that. But I can't seem to make it happen. One thing I learned in the process is that the problem is somehow taking place *before* this module is ever called into action. I pulled my module out of the main app, gave it a little stub to call the appropriate functions, and it works just fine. Razzle-fraggin...

      ---
      A fair fight is a sign of poor planning.

Re: Schrodinger's Variable
by Roger (Parson) on Jan 23, 2004 at 00:32 UTC
    Can you show us a snip of the code? It sounds like the function being called has modified the variables. Unless we see some code, we can only keep guessing. ;-)

      I thought about that possibility. I tried calling other functions, including functions that just print a line of text, and functions that are outside of the current package. Same result no matter what's called.

      ---
      A fair fight is a sign of poor planning.

Re: Schrodinger's Variable
by ysth (Canon) on Jan 23, 2004 at 05:21 UTC
    If your code is very long, you might see if it will fit on your scratchpad (remembering to make it public), and let us know it's there.
Re: Schrodinger's Variable
by etcshadow (Priest) on Jan 23, 2004 at 02:40 UTC
    SHOW THE CODE, PLEASE!

    As Zaxo pointed out... this is probably a scoping issue or an issue with you not understanding the difference between different variables with the same name in different places.

    If you post the code, we can figure this out for you.

    ------------ :Wq Not an editor command: Wq