Yes, but you need something to call UNIVERSAL::VERSION. Usually, that something is Exporter. Proof:
# Module1.pm # ========== package Module1; our $VERSION = '2.0'; 1;
# Module2.pm # ========== package Module2; use Exporter (); our $VERSION = '2.0'; our @ISA = 'Exporter'; 1;
# script.pl # ========= BEGIN { print("Loading Module1...\n"); } use Module1 '3.0'; BEGIN { print("Loaded.\n"); } BEGIN { print("Loading Module2...\n"); } use Module2 '3.0'; BEGIN { print("Loaded.\n"); }
output ====== Loading Module1... Loaded. Loading Module2... Module2 3.0 required--this is only version 2.0 (Module2.pm) at script. +pl line 6 BEGIN failed--compilation aborted at script.pl line 6.

Notice how the version passed to Module1 is completely ignored.


In writing this test, I also found errors in what I thought I knew.

1) *import = \&Exporter::import; doesn't work as well as I thought. My original Module2 gave a run-time error. It was:

package Module2; use Exporter (); our $VERSION = '2.0'; *import = \&Exporter::import; 1;

To fix without inheriting from Exporter, one needs to also import require_version:

package Module2; use Exporter (); our $VERSION = '2.0'; *import = \&Exporter::import; *require_version = \&Exporter::require_version; 1;

2) I always thought use Module 'maj.min' would make sure the major version is the same as the one in $VERSION, yet use Module2 '1.0' doesn't fail.


In reply to Re^4: Finding version of a Perl module by ikegami
in thread Finding version of a Perl module by bradcathey

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.