This allows you to figure out what version of a loaded module you have. This is important if, as is often the case, different versions have different APIs and you need to support multiple APIs.

The code is an example of how to use UNIVERSAL::VERSION(). This is a snippet from the upcoming PDF::Template v0.07.

$self->{PDF_VERSION} = 0; for my $version (reverse 1 .. 4) { eval "UNIVERSAL::VERSION('pdflib_pl', $version.0)"; unless ($@) { $self->{PDF_VERSION} = $version; last; } } die "Cannot find pdflib_pl version",$/ unless $self->{PDF_VERSION};

Replies are listed 'Best First'.
Re: Figuring out which version of a loaded module
by xmath (Hermit) on Feb 26, 2003 at 15:30 UTC
    use MyModule; print MyModule->VERSION;
      I want to find out if MyModule is at least version N. I don't want to parse the version strings. I'll let VERSION do that for me. :-)

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

        if you supply a number to UNIVERSAL::VERSION like you do, it simply does a numeric compare.

        So your exact script could be reduced to:

        $self->{PDF_VERSION} = int pdflib_pl->VERSION or die "Cannot find pdflib_pl version\n"; $self->{PDF_VERSION} = 4 if $self->{PDF_VERSION} > 4;

        •Update: OK, correction, if $pdflib_pl::VERSION is a v-string it messes up, but I have never seen a module that uses that (none of the core modules are, nor are the tons of other modules installed on my computer).

        In fact, perldoc perlmodlib and perlmodstyle actually says explicitly $VERSION should be a floating-point number.