Skip to content

Instantly share code, notes, and snippets.

@skarfacegc
Created June 5, 2015 14:48
Show Gist options
  • Save skarfacegc/5f69a92ea2e891d1575a to your computer and use it in GitHub Desktop.
Save skarfacegc/5f69a92ea2e891d1575a to your computer and use it in GitHub Desktop.
Try to load specific version of a module
#
# Looks for moudules in ./lib with the name of Module-<version>.pl
# Loads the requested version when the module is 'use'ed
#
# Usage:
#
# use LoadModule MyModule => 'v1'
package LoadModule;
use strict;
use warnings;
use Data::Dumper;
my $wantedPackages;
my $INC_LOADED;
sub import
{
my $pkg = shift;
my ($module, $version) = @_;
$wantedPackages->{$module} = $version;
push(@INC, \&loadLib) unless($INC_LOADED);
$INC_LOADED = 1;
}
sub loadLib
{
my ($codeRef, $filename) = @_;
my $moduleName = $filename;
# Strip the implied pm
$moduleName =~ s/\.pm$//;
if(exists $wantedPackages->{$moduleName})
{
my $package = $moduleName . "-" . $wantedPackages->{$moduleName} . ".pm";
if(-e "./lib/$package")
{
my $fh;
open($fh, "<", "./lib/$package") || die "Can't open ./lib/$package: $!";
return($fh);
}
}
else
{
return undef;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment