in reply to when is "my" not necessary?

my creates lexical variables. When you use symbolic references (as in your foreach loop), you are referring to package variables. If you print $main::zero, etc. you will see the results of your assignments. Note that symbolic references are usually a bad thing, and you should almost always use strict.

Update: Some relevant reading material.

Replies are listed 'Best First'.
Re^2: when is "my" not necessary?
by ishnid (Monk) on Nov 02, 2004 at 20:08 UTC
    And when you *do* start using strict, it won't let you use symbolic (soft) references anyway.
Re^2: when is "my" not necessary?
by whillers (Initiate) on Nov 04, 2004 at 18:34 UTC
    OK.. This is driving me crazy. In package x, I have @list = ('a','b','c'). In package y, (after requiring package x), I try: my($a,$b,$c)=(); $a = 'Here'; $b = 'Present': $c = 'Me too'; for(@x::list){ if ($$_) {print "$_ has a value"} } but this does not work using 'my'. I too have moved to stict to support mod_perl (what an excercise!). Why do I do this? I collect form variables (stored in $a,$b,$c) but if they are in @a::list (or better yet, a comma separated database column), they are user-required (and display an error to user if missing). Is this a bad use for symbolic references? What would you do?
      Storing form variables in global variables feels like a very bad idea to me, for the same reason any unrestricted set of global variables is a bad idea.

      Personally I prefer a hash to store the form variables in, CGI.pm does support it, though its a rather new feature, and it's rather well hidden. Look up the method Vars.

        Thanks... ephiphony moment. I'm convinced to use strict refs and eliminate any symbolic usage.

        package x actually is a static cache of prefs stored in a database refreshed only when those prefers are updated by a user.

        Doing my stunt (above), I'm now using a hash to store all form fields and simply checking presence using the x list for that preference.

        Thanks-Thanks.