A much improved and refined sub VERSION() for the implicit approach.

sub VERSION { my ($module, $version) = @_; return $VERSION unless $version; # satisfy UNIVERSAL::VERSION() do { require File::Spec; require File::Basename; my $file = File::Spec->catfile( split /::/, $module ) . ".pm"; my $dir; $dir = File::Basename::dirname( $dir || $INC{$file} ) for 0 .. $module =~ /::/g; for my $dir ( local @INC = grep { !/^$dir$/ && -e File::Spec-> +catfile( $_, $file ) } @INC ) { open MOD, "<", File::Spec->catfile( $dir, $file ) or warn +"$@"; grep { /(.*VERSION.*)/ and eval "eval '$1' == $version" and do { delete $INC{$file}; eval " require $module; $module->VERSION " == $ver +sion and do return $version ; shift @INC; } } <MOD>; } require Carp; Carp::croak(" Loading $module ($version) failed. "); } unless ($version == $VERSION); }
A loader hook for @INC for explicit version control, basically just the above modified slightly.
BEGIN { sub my_use { my ($arg_ref, $module) = @_; my $version; local @INC; (undef, $version, @INC) = @$arg_ref; require File::Spec; for my $dir ( grep { -e File::Spec->catfile( $_, $module ) } @ +INC ) { my $file = File::Spec->catfile( $dir, $module ); open MOD, "<", $file or warn "failed opening $file : $@"; grep { /(?:VERSION\s*=(.*))/ and eval "eval '$1' || 0" == $version and do { open my $fh, "<", $file or warn "open $file failed + : $@"; close MOD; return $fh; } } <MOD>; } } { local @INC = [ \&my_use, "1.03", qw|a b c|, @INC ]; eval "use strict"; } { local @INC = [ \&my_use, "5.01", @INC, qw|a b c|]; eval "use Foo::Bar"; } }
There's bound to be room for correction and improvement, especially to creating a simple and elegant data structure to house information about module and version numbers.

In reply to Re^4: BEGIN and compile-time by Firefly258
in thread BEGIN and compile-time by jbert

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.