Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Import namespace-clean-0.02.tar.gz.

  • Loading branch information...
commit 9b680ffe76a7293ec454dbc07947cd7697520428 1 parent 40aef9d
@rafl authored
View
3  Changes
@@ -1,4 +1,7 @@
+ [0.02] Tue Feb 20 00:38:24 CET 2007
+ - Added unimport behaviour
+
[0.01] Sun Feb 18 17:33:18 CET 2007
- Initial Version
View
2  MANIFEST
@@ -15,8 +15,10 @@ README
t/00-basic.t
t/01-function-wipeout.t
t/02-inheritance.t
+t/03-unimport.t
t/10-pod.t
t/11-pod-coverage.t
t/lib/ExporterTest.pm
t/lib/FunctionWipeout.pm
t/lib/Inheritance.pm
+t/lib/Unimport.pm
View
2  META.yml
@@ -14,4 +14,4 @@ no_index:
requires:
Filter::EOF: 0.02
Symbol: 0
-version: 0.01
+version: 0.02
View
21 README
@@ -2,7 +2,7 @@ NAME
namespace::clean - Keep imports out of your namespace
VERSION
- 0.01
+ 0.02
SYNOPSIS
package Foo;
@@ -17,13 +17,21 @@ SYNOPSIS
sub baz { bar() } # still defined, 'bar' still bound
+ no namespace::clean;
+
+ sub quux { baz() } # will be removed again
+
+ use namespace::clean;
+
### Will print:
# No
# No
# Yes
+ # No
print +(__PACKAGE__->can('croak') ? 'Yes' : 'No'), "\n";
print +(__PACKAGE__->can('bar') ? 'Yes' : 'No'), "\n";
print +(__PACKAGE__->can('baz') ? 'Yes' : 'No'), "\n";
+ print +(__PACKAGE__->can('quux') ? 'Yes' : 'No'), "\n";
1;
@@ -48,6 +56,17 @@ METHODS
Filter::EOF cleanup routine to remove those symbols from the package at
the end of the compile-time.
+ unimport
+ This method will be called when you do a
+
+ no namespace::clean;
+
+ It will start a new section of code that defines functions to clean up.
+
+ get_class_store
+ This returns a reference to a hash in your package containing
+ information about function names included and excluded from removal.
+
get_functions
Takes a class as argument and returns all currently defined functions in
it as a hash reference with the function name as key and a typeglob
View
78 lib/namespace/clean.pm
@@ -9,17 +9,18 @@ namespace::clean - Keep imports out of your namespace
use warnings;
use strict;
-use vars qw( $VERSION );
+use vars qw( $VERSION $STORAGE_VAR );
use Symbol qw( qualify_to_ref );
use Filter::EOF;
=head1 VERSION
-0.01
+0.02
=cut
-$VERSION = 0.01;
+$VERSION = 0.02;
+$STORAGE_VAR = '__NAMESPACE_CLEAN_STORAGE';
=head1 SYNOPSIS
@@ -35,13 +36,21 @@ $VERSION = 0.01;
sub baz { bar() } # still defined, 'bar' still bound
+ no namespace::clean;
+
+ sub quux { baz() } # will be removed again
+
+ use namespace::clean;
+
### Will print:
# No
# No
# Yes
+ # No
print +(__PACKAGE__->can('croak') ? 'Yes' : 'No'), "\n";
print +(__PACKAGE__->can('bar') ? 'Yes' : 'No'), "\n";
print +(__PACKAGE__->can('baz') ? 'Yes' : 'No'), "\n";
+ print +(__PACKAGE__->can('quux') ? 'Yes' : 'No'), "\n";
1;
@@ -76,16 +85,65 @@ sub import {
my $cleanee = caller;
my $functions = $pragma->get_functions($cleanee);
+ my $store = $pragma->get_class_store($cleanee);
- Filter::EOF->on_eof_call(sub {
- for my $f (keys %$functions) {
- next unless $functions->{ $f }
- and *{ $functions->{ $f } }{CODE};
- { no strict 'refs';
+ for my $f (keys %$functions) {
+ next unless $functions->{ $f }
+ and *{ $functions->{ $f } }{CODE};
+ $store->{remove}{ $f } = 1;
+ }
+
+ unless ($store->{handler_is_installed}) {
+ Filter::EOF->on_eof_call(sub {
+ for my $f (keys %{ $store->{remove} }) {
+ next if $store->{exclude}{ $f };
+ no strict 'refs';
delete ${ "${cleanee}::" }{ $f };
}
- }
- });
+ });
+ $store->{handler_is_installed} = 1;
+ }
+
+ return 1;
+}
+
+=head2 unimport
+
+This method will be called when you do a
+
+ no namespace::clean;
+
+It will start a new section of code that defines functions to clean up.
+
+=cut
+
+sub unimport {
+ my ($pragma) = @_;
+
+ my $cleanee = caller;
+ my $functions = $pragma->get_functions($cleanee);
+ my $store = $pragma->get_class_store($cleanee);
+
+ for my $f (keys %$functions) {
+ next if $store->{remove}{ $f }
+ or $store->{exclude}{ $f };
+ $store->{exclude}{ $f } = 1;
+ }
+
+ return 1;
+}
+
+=head2 get_class_store
+
+This returns a reference to a hash in your package containing information
+about function names included and excluded from removal.
+
+=cut
+
+sub get_class_store {
+ my ($pragma, $class) = @_;
+ no strict 'refs';
+ return \%{ "${class}::${STORAGE_VAR}" };
}
=head2 get_functions
View
21 t/03-unimport.t
@@ -0,0 +1,21 @@
+#!/usr/bin/env perl
+use warnings;
+use strict;
+
+use FindBin;
+use lib "$FindBin::Bin/lib";
+use Test::More tests => 6;
+
+use_ok('Unimport');
+
+ok( !Unimport->can('foo'),
+ 'first function correctly removed' );
+ok( Unimport->can('bar'),
+ 'excluded method still in package' );
+ok( !Unimport->can('baz'),
+ 'second function correctly removed' );
+ok( Unimport->can('qux'),
+ 'last method still in package' );
+is( Unimport->qux, 23,
+ 'all functions are still bound' );
+
View
19 t/lib/Unimport.pm
@@ -0,0 +1,19 @@
+package Unimport;
+use warnings;
+use strict;
+
+sub foo { 23 }
+
+use namespace::clean;
+
+sub bar { foo() }
+
+no namespace::clean;
+
+sub baz { bar() }
+
+use namespace::clean;
+
+sub qux { baz() }
+
+1;
Please sign in to comment.
Something went wrong with that request. Please try again.