On Tue, Oct 28, 2003 at 10:52:16AM -0600, Dan Muey wrote: > I want to use the newer our $variable; but to make it work > with pre 5.6 Perl (or whatever version our appeared in) I > have to do the use vars qw($variable); method > > So I was wanting some input about pros and cons of using > either since on the surface they appear to do the same thing > except that use vars is backwards compatable.
"use vars" and "our" do roughly the same thing. They both let you use package variables under strict without fully-qualifying. All these code snippets pass strict, and they each set the package variable $foo ($A::foo, $B::foo, and $C::foo). use strict; { package A; use vars qw($foo); $foo = 'A::foo'; } { package B; our $foo; $foo = 'B::foo'; } { package C; $C::foo = 'C::foo'; # fully-qualifying } The main practical difference is, like you said, that "our" is new in 5.6. So if you want to support perl5.005_03 (which is still in relatively wide use) you have to "use vars" instead. But "use vars" and "our" work differently, and there are some subtle differences in their behavior. First of all, "use vars" does its magic by twiddling the symbol table, marking the $foo variable as "imported". Since the symbol table is global, you can use $foo any time you are in the package that imported it -- even if you're in a different block or file. package A; use strict; { use vars qw($foo) } $foo = 'A::foo'; # different block; no problem And for the same reason, a "use vars" declaration doesn't extend across two packages in the same block or file. package A; use strict; use vars qw($foo); package B; $foo = 'B::foo'; # different package: ERROR On the other hand, "our" does its magic by creating a lexical symbol (like a "my" variable) that is aliased to the package variable of the same name. Since lexical symbols are only visible in the scope (block, file, or eval) where they were declared, the "our" declaration doesn't exist outside this block. package A; use strict; { our $foo; } $foo = 'A::foo'; # different lexical scope: ERROR And when you have several packages in the same lexical scope, the "our" declaration extends across them. package A; use strict; our $foo; # alias to $A::foo package B; $foo = 'A::foo'; # STILL refers to $A::foo -- Steve -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]