Skip to content

Commit

Permalink
Implement and test use perl5i::2 -skip => [feature1, feature2];
Browse files Browse the repository at this point in the history
Needs docs.
  • Loading branch information
schwern committed Feb 26, 2013
1 parent 8480bb6 commit 024b324
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
26 changes: 23 additions & 3 deletions lib/perl5i/2.pm
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use perl5i::VERSION; our $VERSION = perl5i::VERSION->VERSION;
our $Latest = perl5i::VERSION->latest;

my %Features = (
# A stub for autodie. It's handled specially in import().
autodie => sub {},
autobox => sub {
my ($class, $caller) = @_;

Expand Down Expand Up @@ -142,18 +144,36 @@ use parent 'autodie';
## no critic (Subroutines::RequireArgUnpacking)
sub import {
my $class = shift;
my %import = @_;

my $caller = caller;

# Read the skip list and turn it into a hash
my $skips = delete $import{-skip} || [];
$skips = { map { $_ => 1 } @$skips };

# Any remaining import parameters are unknown
if( keys %import ) {
croak sprintf "Unknown parameters '%s' in import list",
join(", ", map { "$_ => $import{$_}" } keys %import);
}

# Check all the skipped features are valid
for my $f ( grep { !exists $Features{$_} } keys %$skips ) {
croak "Unknown feature '$f' in skip list";
}

# Current lexically active major version of perl5i.
$^H{perl5i} = 2;

for my $feature (values %Features) {
$feature->($class, $caller);
# Load all the features.
for my $feature (keys %Features) {
next if $skips->{$feature};
$Features{$feature}->($class, $caller);
}

# autodie needs a bit more convincing
if( 1 ) {
if( !$skips->{autodie} ) {
@_ = ( $class, ":all" );
goto &autodie::import;
}
Expand Down
43 changes: 43 additions & 0 deletions t/skip.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env perl

use Test::More;

note "skipping a feature";
{
# Needs its own package because perl5i is not always lexical
package Foo1;

use perl5i::latest -skip => ['Signatures'];

::ok !eval q[method foo { 42 }; 1];
::ok !defined &foo;
}

note "skipping autodie";
{
# Needs its own package because perl5i is not always lexical
package Foo2;

use perl5i::latest -skip => ["autodie"];
open my $fh, "/i/do/not/exist/alfkjaldjlf";
::pass("autodie is disabled");
}

note "unknown feature error";
{
my $feature = 'Orbital Mind Control Lasers';
ok !eval {
perl5i::latest->import( -skip => [$feature] );
};
is $@, sprintf "Unknown feature '%s' in skip list at %s line %d.\n", $feature, $0, __LINE__-2;
}

note "unknown import parameter";
{
ok !eval {
perl5i::latest->import( wibble => "what" );
};
is $@, sprintf "Unknown parameters 'wibble => what' in import list at %s line %d.\n", $0, __LINE__-2;
}

done_testing;

0 comments on commit 024b324

Please sign in to comment.