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

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.

Replies are listed 'Best First'.
Re^3: What does use vars qw{$VERSION}; do?
by davido (Cardinal) on Oct 31, 2016 at 05:15 UTC

    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.
Re^3: What does use vars qw{$VERSION}; do?
by ikegami (Patriarch) on Nov 02, 2016 at 16:25 UTC

    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.