Skip to content

Commit

Permalink
Tidied.
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyA committed Mar 11, 2012
1 parent d1bcf26 commit b0b7e4c
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 61 deletions.
78 changes: 39 additions & 39 deletions lib/Devel/Unplug.pm
Expand Up @@ -35,25 +35,25 @@ C<use>) and intercept attempts to load modules.
=cut

sub _get_module {
my $file = shift;
$file =~ s{/}{::}g;
$file =~ s/[.]pm$//;
return $file;
my $file = shift;
$file =~ s{/}{::}g;
$file =~ s/[.]pm$//;
return $file;
}

my %unplugged;

sub _is_unplugged {
my $module = shift;
my $module = shift;

for my $unp ( unplugged() ) {
return 1
if ( 'Regexp' eq ref $unp )
? $module =~ $unp
: $module eq $unp;
}
for my $unp ( unplugged() ) {
return 1
if ( 'Regexp' eq ref $unp )
? $module =~ $unp
: $module eq $unp;
}

return;
return;
}

=head1 INTERFACE
Expand All @@ -74,11 +74,11 @@ Regular expressions may be used:
=cut

sub unplug {
for my $unp ( @_ ) {
exists $unplugged{$unp} and $unplugged{$unp}->[1]++
or $unplugged{$unp} = [ $unp, 1 ];
}
return;
for my $unp ( @_ ) {
exists $unplugged{$unp} and $unplugged{$unp}->[1]++
or $unplugged{$unp} = [ $unp, 1 ];
}
return;
}

=head2 C<< insert >>
Expand All @@ -93,28 +93,28 @@ was called to make it available again.
=cut

sub insert {
for my $mod ( @_ ) {
delete $unplugged{$mod}
if exists $unplugged{$mod} && 0 == --$unplugged{$mod}->[1];
}
return;
for my $mod ( @_ ) {
delete $unplugged{$mod}
if exists $unplugged{$mod} && 0 == --$unplugged{$mod}->[1];
}
return;
}

BEGIN {
use Devel::TraceLoad::Hook qw( register_require_hook );
register_require_hook(
sub {
my ( $when, $depth, $arg, $p, $f, $l, $rc, $err ) = @_;

return unless $when eq 'before';
my $module = _get_module( $arg );
return unless _is_unplugged( $module );

# Ain't gonna let you load it
die "Can't locate $arg in \@INC (unplugged by "
. __PACKAGE__ . ")";
}
);
use Devel::TraceLoad::Hook qw( register_require_hook );
register_require_hook(
sub {
my ( $when, $depth, $arg, $p, $f, $l, $rc, $err ) = @_;

return unless $when eq 'before';
my $module = _get_module( $arg );
return unless _is_unplugged( $module );

# Ain't gonna let you load it
die "Can't locate $arg in \@INC (unplugged by "
. __PACKAGE__ . ")";
}
);
}

=head2 C<< unplugged >>
Expand All @@ -125,12 +125,12 @@ contain a mixture of regular expressions and plain strings.
=cut

sub unplugged {
map { $_->[0] } values %unplugged;
map { $_->[0] } values %unplugged;
}

sub import {
my $class = shift;
unplug( @_ );
my $class = shift;
unplug( @_ );
}

1;
Expand Down
12 changes: 6 additions & 6 deletions lib/Devel/Unplug/OO.pm
Expand Up @@ -54,15 +54,15 @@ be re-inserted.
=cut

sub new {
my $class = shift;
my $self = bless [@_], $class;
Devel::Unplug::unplug( @$self );
return $self;
my $class = shift;
my $self = bless [@_], $class;
Devel::Unplug::unplug( @$self );
return $self;
}

sub DESTROY {
my $self = shift;
Devel::Unplug::insert( @$self );
my $self = shift;
Devel::Unplug::insert( @$self );
}

1;
Expand Down
4 changes: 2 additions & 2 deletions t/000-load.t
@@ -1,8 +1,8 @@
use Test::More tests => 2;

BEGIN {
use_ok( 'Devel::Unplug' );
use_ok( 'Devel::Unplug::OO' );
use_ok( 'Devel::Unplug' );
use_ok( 'Devel::Unplug::OO' );
}

diag( "Testing Devel::Unplug $Devel::Unplug::VERSION" );
16 changes: 9 additions & 7 deletions t/commandline.t
Expand Up @@ -9,14 +9,16 @@ my @nop = ( '-e', '0' );

like tryit( @cmd, '-MSome::Module', @nop ), qr{^\s*$}, "no error";

like tryit( @cmd, '-Ilib', '-MDevel::Unplug=Some::Module', '-MSome::Module', @nop ),
qr{Can't\s+locate\s+Some/Module.pm}, "error message";
like tryit( @cmd, '-Ilib', '-MDevel::Unplug=Some::Module',
'-MSome::Module', @nop ),
qr{Can't\s+locate\s+Some/Module.pm}, "error message";

like tryit( @cmd, '-Ilib', '-MDevel::Unplug=Some::Module', '-MSome::Other::Module', @nop ),
qr{^\s*$}, "no crosstalk";
like tryit( @cmd, '-Ilib', '-MDevel::Unplug=Some::Module',
'-MSome::Other::Module', @nop ),
qr{^\s*$}, "no crosstalk";

sub tryit {
my @cmd = @_;
run \@cmd, \my $in, \my $out, \my $err, timeout( 10 );
return $err;
my @cmd = @_;
run \@cmd, \my $in, \my $out, \my $err, timeout( 10 );
return $err;
}
8 changes: 4 additions & 4 deletions t/nest.t
Expand Up @@ -6,10 +6,10 @@ use Devel::Unplug;
use lib 't/lib';

sub is_unplugged($;$) {
my ( $list, $desc ) = @_;
$desc ||= join( ', ', @$list );
my @unp = Devel::Unplug::unplugged();
is_deeply [ sort @unp ], [ sort @$list ], $desc;
my ( $list, $desc ) = @_;
$desc ||= join( ', ', @$list );
my @unp = Devel::Unplug::unplugged();
is_deeply [ sort @unp ], [ sort @$list ], $desc;
}

Devel::Unplug::unplug( 'Some::Module', qr{^Other::} ) for 1 .. 2;
Expand Down
5 changes: 3 additions & 2 deletions t/pod-coverage.t
Expand Up @@ -2,6 +2,7 @@

use Test::More;
eval "use Test::Pod::Coverage 1.04";
plan skip_all => "Test::Pod::Coverage 1.04 required for testing POD coverage"
if $@;
plan skip_all =>
"Test::Pod::Coverage 1.04 required for testing POD coverage"
if $@;
all_pod_coverage_ok( { private => [ qr{^import|DESTROY$}, qr{^_} ] } );
2 changes: 1 addition & 1 deletion t/wildcard.t
Expand Up @@ -16,7 +16,7 @@ eval "use Some::Other::Module";
like $@, qr{Can't\s+locate\s+Some/Other/Module.pm}, "error message";

my @unp = Devel::Unplug::unplugged();
is_deeply \@unp, [ $match ], "unplugged OK";
is_deeply \@unp, [$match], "unplugged OK";

Devel::Unplug::insert( $match );
eval "use Some::Module";
Expand Down

0 comments on commit b0b7e4c

Please sign in to comment.