NAME
namespace::sweep - Sweep up imported subs in your classes
VERSION
version 0.006
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!
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.
ARGUMENTS
The following arguments may be passed on the use
line:
- -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
- -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
$_
. 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_.+/ ];
RATIONALE
This pragma was written to address some problems with the excellent namespace::autoclean. In particular, namespace::autoclean will remove special symbols that are installed by overload, so you can't use namespace::autoclean on objects that overload Perl operators.
Additionally, namespace::autoclean relies on Class::MOP to figure out the list of methods provided by your class. This pragma does not depend on Class::MOP or 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) 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.
CAVEATS
This is an early release and there are bound to be a few hiccups along the way.
ACKNOWLEDGEMENTS
Thanks Florian Ragwitz and Tomas Doran for writing and maintaining namespace::autoclean.
Thanks to Toby Inkster for submitting some better code for finding meta
objects.
SEE ALSO
namespace::autoclean, namespace::clean, overload
AUTHOR
Mike Friedman <friedo@friedo.com>
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.