Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

use vars vs our vs $main:: for $VERSION

by bv (Friar)
on Sep 25, 2009 at 15:59 UTC ( [id://797544] : perlquestion . print w/replies, xml ) Need Help??

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

So I wanted to use Getopt::Long's auto_version feature on a script that I use. It requires a package variable named $VERSION to be set, and I have at least three options for doing so:

  • use vars qw($VERSION); $VERSION = 1.3;
  • our $VERSION = 1.3;
  • $main::VERSION = 1.3;

I have verified that all of these work, and I understand that the first two allow me to refer to the variable as $VERSION within package and lexical scope, respectively. My question is: Is any one of these preferable? I don't plan on referring to the version number anywhere else in the script.

print pack("A25",pack("V*",map{1919242272+$_}(34481450,-49737472,6228,0,-285028276,6979,-1380265972)))

Replies are listed 'Best First'.
Re: use vars vs our vs $main:: for $VERSION
by cdarke (Prior) on Sep 25, 2009 at 16:28 UTC
    It is somewhat personal taste, but with that in mind:
    our $VERSION = 1.3;
    is my choice. our was introduced in 5.6, so it has been around long enough for people to know what it means.
Re: use vars vs our vs $main:: for $VERSION
by toolic (Bishop) on Sep 25, 2009 at 17:06 UTC
    The documentation for vars states:
    vars - Perl pragma to predeclare global variable names (obsolete)
    So, you can probably cross that off your list.
Re: use vars vs our vs $main:: for $VERSION
by afoken (Chancellor) on Sep 26, 2009 at 10:09 UTC

    use vars qw($VERSION);$VERSION=1.3; is needed only if you want compatibility with ancient versions of perl 5 (older than 5.6.0).

    our $VERSION=1.3; works perfectly with any perl since 5.6.0 (released 9.5 years ago).

    $main::VERSION=1.3; is just wrong, unless you want to set the VERSION of the main package. Did you mean $What::Ever::Package::VERSION=1.3;? That should work with every perl 5.x, but needs more typing.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      Since the script is standalone (not a module), the $main::VERSION one isn't completely wrong, but I do understand that our is better, since it does the right thing if I am in package main or any other package. Thanks!

      print pack("A25",pack("V*",map{1919242272+$_}(34481450,-49737472,6228,0,-285028276,6979,-1380265972)))
Re: use vars vs our vs $main:: for $VERSION
by JavaFan (Canon) on Sep 29, 2009 at 10:22 UTC
    Is any one of these preferable?
    By all means, do whatever you find most preferable. You're likely the one who's going to deal with the code most of the time (and even then, you're likely won't change anything other than the version number itself). And anyone else isn't going to get confused when seeing a line that assigns a number to a variable called VERSION. It's obvious what it's for, regardless of the syntax you're using.

    One point though, $main::VERSION sets the variable in the package main. I presume you're using main just as an example - normally you want to use the name of the package instead of main. Otherwise, Exporter cannot do its job.

    I prefer the our $VERSION = "..."; style, but I can happily work with the other styles. I would use use vars '$VERSION'; $VERSION = "..."; if I had to create a new module that needs to be able to run on 5.005, but it has been many years since I created a module with such a restriction, and I doubt I will write another such module in the future.