Declaring and using global variables

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
It's a bit late and I'm a bit tired, I've never used "our" and rarely
used "use vars", and I want to have the best style.

At the $ORKPLACE, we do "use strict;" and "use warnings;".  We usually
don't use global variables, but it occasionally happens.  While
restructuring the code to use accessor functions may be a more
virtuous action, I can't restructure the code -- I'd just like to
clean up a few things from the output of Perl::Critic.  Our Perl
environment is well past 5.6.

We sometimes refer to dynamic variables in the same file and sometimes
in other files.  Some package Adventure::Cave may use "use vars
qw(...)" to "declare" dynamic variables for that package, and other
packages "use Adventure::Cave;" and then refer to variables as
"$Adventure::cave::whatever".  We don't do exporting or importing.
That is, if you want a more concrete (contrived) example,

    package Adventure::Cave;
    use strict;
    use warnings;
    use vars qw(
    $lamp = 1;
    $food = 2;
    $water = 3;
    sub whatnot {
       if ($object == $lamp) { ... }

and in another file

    package Player;
    use strict;
    use warnings;
    use Adventure::Cave;
    sub whatever {
        if ($thingy == $Adventure::Cave::lamp) { ... }

Can I and should I just replace
    use vars qw($lamp $food $water);
    our ($lamp, $food, $water);
?  We don't have more than one package in a file and we do "use vars"
only at top level, so I think the difference in scoping between the
two should not matter.

Other than "use Adventure::Cave;", should I be doing something in
Player before referring to $Adventure::Cave::lamp?
Perl::Critic::Policy::Variables::ProhibitPackageVars complains, but I
don't like to do "## no critic" if there's a right way to avoid it.

Tim McDaniel,

Re: Declaring and using global variables

On 8/7/2014 23:32, Tim McDaniel wrote:
Quoted text here. Click to load it

Don't see why not.  I seldom use 'use vars' in my code - I think of it
more like a kludge.

Quoted text here. Click to load it

Like what?

A simple little test case should answer your questions.

perlfunc man page:

     our EXPR
     our TYPE EXPR
     our EXPR : ATTRS
     our TYPE EXPR : ATTRS
         "our" associates a simple name with a package variable in the current
         package for use within the current scope. When "use strict 'vars'" is in
         effect, "our" lets you use declared global variables without qualifying
         them with package names, within the lexical scope of the "our"
         declaration. In this way "our" differs from "use vars", which is package

         Unlike "my", which both allocates storage for a variable and associates
         a simple name with that storage for use within the current scope, "our"
         associates a simple name with a package variable in the current package,
         for use within the current scope. In other words, "our" has the same
         scoping rules as "my", but does not necessarily create a variable.

         If more than one value is listed, the list must be placed in

             our $foo;
             our($bar, $baz);

         An "our" declaration declares a global variable that will be visible
         across its entire lexical scope, even across package boundaries. The
         package in which the variable is entered is determined at the point of
         the declaration, not at the point of use.

Site Timeline