summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/Package/Stash.pm35
-rw-r--r--t/bug-rt-78272.t4
-rw-r--r--t/impl-selection/env.t10
3 files changed, 20 insertions, 29 deletions
diff --git a/lib/Package/Stash.pm b/lib/Package/Stash.pm
index 08a5e13..8a3fa9f 100644
--- a/lib/Package/Stash.pm
+++ b/lib/Package/Stash.pm
@@ -6,35 +6,16 @@ use 5.008001;
our $IMPLEMENTATION;
+use Module::Implementation;
+
BEGIN {
- $IMPLEMENTATION = $ENV{PACKAGE_STASH_IMPLEMENTATION}
- if exists $ENV{PACKAGE_STASH_IMPLEMENTATION};
-
- my $err;
- if ($IMPLEMENTATION) {
- my $file = "Package::Stash::$IMPLEMENTATION.pm";
- $file =~ s{::}{/}g;
- if (!eval 'require($file) ; 1') {
- require Carp;
- Carp::croak("Could not load Package::Stash::$IMPLEMENTATION: $@");
- }
- }
- else {
- for my $impl ('XS', 'PP') {
- if (eval "require Package::Stash::$impl; 1;") {
- $IMPLEMENTATION = $impl;
- last;
- }
- else {
- $err .= $@;
- }
- }
- }
+ local $ENV{PACKAGE_STASH_IMPLEMENTATION} = $IMPLEMENTATION
+ if ( $IMPLEMENTATION and not $ENV{PACKAGE_STASH_IMPLEMENTATION} );
- if (!$IMPLEMENTATION) {
- require Carp;
- Carp::croak("Could not find a suitable Package::Stash implementation: $err");
- }
+ Module::Implementation::build_loader_sub(
+ implementations => [ 'XS', 'PP' ]
+ )->();
+ $IMPLEMENTATION = Module::Implementation::implementation_for(__PACKAGE__);
my $impl = "Package::Stash::$IMPLEMENTATION";
my $from = $impl->new($impl);
diff --git a/t/bug-rt-78272.t b/t/bug-rt-78272.t
index c2bfff4..0d6099b 100644
--- a/t/bug-rt-78272.t
+++ b/t/bug-rt-78272.t
@@ -10,7 +10,7 @@ subtest 'Bug RT-78272: Arbitrary code execution from $ENV' => sub {
like(
exception { require Package::Stash },
- qr/^Could not load Package::Stash::$e/,
+ qr/$e is not a valid implementation for Package::Stash/,
'Arbitrary code in $ENV throws exception'
);
@@ -19,7 +19,7 @@ subtest 'Bug RT-78272: Arbitrary code execution from $ENV' => sub {
delete $INC{'Package/Stash.pm'};
require Package::Stash;
},
- qr/^Could not load Package::Stash::$e/,
+ qr/$e is not a valid implementation for Package::Stash/,
'Sanity check: forcing package reload throws the exception again'
);
diff --git a/t/impl-selection/env.t b/t/impl-selection/env.t
index 3369488..c050267 100644
--- a/t/impl-selection/env.t
+++ b/t/impl-selection/env.t
@@ -26,4 +26,14 @@ SKIP: {
can_ok('Package::Stash', 'new');
}
+{
+ delete $Package::{'Stash::'};
+ delete $INC{'Package/Stash.pm'};
+ set_impl('INVALID');
+ $ENV{PACKAGE_STASH_IMPLEMENTATION} = 'PP';
+ require Package::Stash;
+ is(get_impl, 'PP', '$ENV takes precedence over $Package::Stash::IMPLEMENTATION');
+ can_ok('Package::Stash', 'new');
+}
+
done_testing;