Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: What does use vars qw{$VERSION}; do?

by kevbot (Vicar)
on Oct 31, 2016 at 04:33 UTC ( [id://1174987]=note: print w/replies, xml ) Need Help??


in reply to What does use vars qw{$VERSION}; do?

It creates a global variable named $VERSION. The use of a BEGIN block ensures the variable is set before other code in the script has run. See the documentation for BEGIN and use vars. Here is a nice introduction to BEGIN blocks. Note that use vars has been superseded by our. This use vars vs our vs $main:: for $VERSION node has a discussion that you may find helpful.

Replies are listed 'Best First'.
Re^2: What does use vars qw{$VERSION}; do?
by alwynpan (Acolyte) on Oct 31, 2016 at 05:09 UTC
    Thank you for your prompt reply. So this will be the same as?
    our $VERSION; BEGIN { $VERSION = '0.92'; }
    But I can't see the variable $VERSION is used in this file, can I assume this is used in some other files? Sorry, this is my first day in the Perl world, my question may be very silly.

      All CPAN modules should (and many other non-CPAN modules also do) declare a $VERSION. This allows the caller to do this:

      use Foo; if (Foo->VERSION() < 1.56) { # do something... } # or if ($Foo::VERSION < 1.56) { # do something... }

      (Take your pick.)... or...

      use Foo 1.56 # Die during compiletime if the minimum version is not me +t.

      In most cases the module that declares a version doesn't use it for anything itself. The package global is set up for external consumption. This is one of the benefits of package globals: Anyone from any other package can reach right in and have a look.

      Here's another example:

      perl -MList::Util -E 'say List::Util->VERSION()'

      The BEGIN block, once again, is used to assure that the variable is given a value early enough in the compile process that it will be available to the caller when the caller invokes use. It also turns out that use can be used to enforce minimum versions, so the variable MUST have a value early in the process. See use for additional explanation of version checking, and perlmod and perlmodstyle for discussion of version numbers.


      Dave

        Hi Dave, crystal clear. Thank you so much for your detailed explanation.

      Yeah, you could use that instead (since Perl 5.6). There's a corner case in which they are different, but it's nothing to worry about.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1174987]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-03-29 10:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found