/usr/share/perl5/namespace/sweep.pm is in libnamespace-sweep-perl 0.006-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | package namespace::sweep;
{
$namespace::sweep::VERSION = '0.006';
}
# ABSTRACT: Sweep up imported subs in your classes
use strict;
use warnings;
use Scalar::Util 'blessed', 'reftype';
use List::Util 'first';
use Carp 'croak';
use Data::Dumper;
use Sub::Identify 0.04 'get_code_info';
use B::Hooks::EndOfScope 0.09 'on_scope_end';
use Package::Stash 0.33;
$namespace::sweep::AUTHORITY = 'cpan:FRIEDO';
sub import {
my ( $class, %args ) = @_;
my $cleanee = exists $args{-cleanee} ? $args{-cleanee} : scalar caller;
my @alsos;
if ( exists $args{-also} ) {
if ( ref $args{-also} && ( reftype $args{-also} eq reftype [ ] ) ) {
@alsos = @{ $args{-also} };
} else {
@alsos = ( $args{-also} );
}
}
my @also_tests;
foreach my $also( @alsos ) {
my $test = !$also ? sub { 0 }
: !ref( $also ) ? sub { $_[0] eq $also }
: reftype $also eq reftype sub { } ? sub { local $_ = $_[0]; $also->() }
: reftype $also eq reftype qr// ? sub { $_[0] =~ $also }
: croak sprintf q{Don't know what to do with [%s] for -also}, $also;
push @also_tests, $test;
}
my $run_test = sub {
return 1 if first { $_->( $_[0] ) } @also_tests;
return;
};
on_scope_end {
no strict 'refs';
my $st = $cleanee . '::';
my $ps = Package::Stash->new( $cleanee );
my $sweep = sub {
# stolen from namespace::clean
my @symbols = map {
my $name = $_ . $_[0];
my $def = $ps->get_symbol( $name );
defined($def) ? [$name, $def] : ()
} '$', '@', '%', '';
$ps->remove_glob( $_[0] );
$ps->add_symbol( @$_ ) for @symbols;
};
my %keep;
my $class_of_cm = UNIVERSAL::can('Class::MOP', 'can') && 'Class::MOP'->can('class_of');
my $class_of_mu = UNIVERSAL::can('Mouse::Util', 'can') && 'Mouse::Util'->can('class_of');
if ( $class_of_cm or $class_of_mu ) {
# look for moose-ish composed methods
my ($meta) =
grep { !!$_ }
map { $cleanee->$_ }
grep { defined $_ }
($class_of_cm, $class_of_mu);
if ( blessed $meta && $meta->can( 'get_all_method_names' ) ) {
%keep = map { $_ => 1 } $meta->get_all_method_names;
}
}
foreach my $sym( keys %{ $st } ) {
$sweep->( $sym ) and next if $run_test->( $sym );
next unless exists &{ $st . $sym };
next if $keep{$sym};
my ( $pkg, $name ) = get_code_info \&{ $st . $sym };
next if $pkg eq $cleanee; # defined in the cleanee pkg
next if $pkg eq 'overload' and $name eq 'nil'; # magic overload method
$sweep->( $sym );
}
};
}
1;
__END__
=pod
=head1 NAME
namespace::sweep - Sweep up imported subs in your classes
=head1 VERSION
version 0.006
=head1 SYNOPSIS
package Foo;
use namespace::sweep;
use Some::Module qw(some_function);
sub my_method {
my $foo = some_function();
...
}
package main;
Foo->my_method; # ok
Foo->some_function; # ERROR!
=head1 DESCRIPTION
Because Perl methods are just regular subroutines, it's difficult to tell what's a method
and what's just an imported function. As a result, imported functions can be called as
methods on your objects. This pragma will delete imported functions from your class's
symbol table, thereby ensuring that your interface is as you specified it. However,
code inside your module will still be able to use the imported functions without any
problems.
=encoding utf-8
=head1 ARGUMENTS
The following arguments may be passed on the C<use> line:
=over
=item -cleanee
If you want to clean a different class than the one importing this pragma, you can
specify it with this flag. Otherwise, the importing class is assumed.
package Foo;
use namespace::sweep -cleanee => 'Bar' # sweep up Bar.pm
=item -also
This lets you provide a mechanism to specify other subs to sweep up that would not
normally be caught. (For example, private helper subs in your module's class that
should not be called as methods.)
package Foo;
use namespace::sweep -also => '_helper'; # sweep up single sub
use namespace::sweep -also => [qw/foo bar baz/]; # list of subs
use namespace::sweep -also => qr/^secret_/; # subs matching regex
You can also specify a subroutine reference which will receive the symbol name as
C<$_>. If the sub returns true, the symbol will be swept.
# sweep up those rude four-letter subs
use namespace::sweep -also => sub { return 1 if length $_ == 4 }
You can also combine these methods into an array reference:
use namespace::sweep -also => [ 'string', sub { 1 if /$pat/ and $_ !~ /$other/ }, qr/^foo_.+/ ];
=back
=head1 RATIONALE
This pragma was written to address some problems with the excellent L<namespace::autoclean>.
In particular, namespace::autoclean will remove special symbols that are installed by
L<overload>, so you can't use namespace::autoclean on objects that overload Perl operators.
Additionally, namespace::autoclean relies on L<Class::MOP> to figure out the list of methods
provided by your class. This pragma does not depend on Class::MOP or L<Moose>, so you can
use it for non-Moose classes without worrying about heavy dependencies.
However, if your class has a Moose (or Moose-compatible) C<meta> object, then that will be
used to find e.g. methods from composed roles that should not be deleted.
In most cases, namespace::sweep should work as a drop-in replacement for namespace::autoclean.
Upon release, this pragma passes all of namespace::autoclean's tests, in addition to its own.
=head1 CAVEATS
This is an early release and there are bound to be a few hiccups along the way.
=head1 ACKNOWLEDGEMENTS
Thanks Florian Ragwitz and Tomas Doran for writing and maintaining namespace::autoclean.
Thanks to Toby Inkster for submitting some better code for finding C<meta> objects.
=head1 SEE ALSO
L<namespace::autoclean>, L<namespace::clean>, L<overload>
=head1 AUTHOR
Mike Friedman <friedo@friedo.com>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2011 by Mike Friedman.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|