Paul Kraus wrote: > Maybe I am misunderstanding but that seems to be the exact same thing > my does.
No. my() creates a new variable. our() refers to a global variable. Try this: use strict; print "Global \$main::foo is at ", \$main::foo, "\n"; our $foo = 'One'; print "a: addr=", \$foo, ", val=$foo\n"; { our $foo = 'Two'; print "b: addr=", \$foo, ", val=$foo\n"; } print "c: addr=", \$foo, ", val=$foo\n"; That produces output like: Global $main::foo is at SCALAR(0x81017b4) a: addr=SCALAR(0x81017b4), val=One b: addr=SCALAR(0x81017b4), val=Two c: addr=SCALAR(0x81017b4), val=Two Note that there's only one variable involved, the global $main::foo. "our" simply lets us refer to it without the main:: qualifier and have use strict no complain. It does not create any new variables. Now, change the "our" declarations to "my" and re-run. That produces output like: Global $main::foo is at SCALAR(0x81017b4) a: addr=SCALAR(0x81017c0), val=One b: addr=SCALAR(0x8103854), val=Two c: addr=SCALAR(0x81017c0), val=One Each "my" has created an entirely new variable. Inside the block, the first "my" variable is hidden, so we only see the second one. Once the block ends, the second variable goes out of scope, and the first one can be seen again. The global, $main::foo is separate from either of these. > > -----Original Message----- > From: Bob Showalter [mailto:[EMAIL PROTECTED]] > Sent: Tuesday, January 21, 2003 11:17 AM > To: '[EMAIL PROTECTED]' > Subject: RE: My, our, local > > > Paul Kraus wrote: > > Great article. Cleared up all of my confusion. However it did not > > touch on "our". Anyone care to explain what its used for. > > "our" provides a lexical scope (from the point of "our" to the end of > the enclosing block or file) within which a global variable may be > used without a package qualifier, even under "use strict". > > It replaces the old vars pragma, which always had file scoping, IIRC. > > use strict; > > our $foo; > > $foo = 1; # OK > > { > our $bar; > $bar = 2; # OK > } > > $bar = 2; # ERROR: outside scope of "our" declaration > > HTH -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]