namespace::functions - Keep package's namespace clean
package My::Class; # import some function use Scalar::Util 'looks_like_number'; # collect the names of all previously created functions use namespace::functions; # our function uses imported function sub is_num { my ($self, $val) = @_; return looks_like_number("$val"); } # delete all previously collected functions no namespace::functions; # our package doesn't provide imported function anymore!
This pragma allows to collect all functions existing in package's namespace and finally delete them.
The problem is that any function which is imported to your package, stays a part of public API of this package. I.e.:
package My::PollutedClass; use Scalar::Util 'looks_like_number'; sub is_num { my ($val) = @_; return looks_like_number("$val"); } package main; print My::PollutedClass->can('looks_like_number'); # true
Deleting imported function from package's stash is a solution, because the function will be not available at run-time:
delete {\%My::PoorSolutionClass::}->{looks_like_number};
The namespace::functions collects the function names and finally deletes them from package's namespace.
namespace::functions
Collects all functions from our namespace.
Collects all functions from our namespace without listed functions.
Deletes all previously collected functions from our namespace.
Deletes all previously collected functions and also additional functions from our namespace.
This pragma needs to be placed in the right order: after last use which imports some unwanted functions and before first sub.
use
sub
package My::Example; use Carp 'confess'; use Scalar::Util 'blessed' use namespace::functions; sub some_method { my ($self) = @_; confess("Call as a method") if not blessed($self); # ... }; no namespace::functions;
You can check if your package is clean with Class::Inspector module and its methods: functions and methods.
functions
methods
use My::Example; use Class::Inspector; use YAML; print Dump ( { functions => [Class::Inspector->functions("My::Example")], methods => [Class::Inspector->methods("My::Example")], } );
Moose keywords can be unexported with no Moose statement. Even that, Moose imports following functions: blessed, confess, meta. The meta method is required by Moose framework and should be unchanged. The others can be deleted safely.
no Moose
blessed
confess
meta
package My::MooseClass; use Moose; use namespace::functions -except => 'meta'; sub my_method { my ($self, $arg) = @_; return blessed $self; }; no namespace::functions; # The My::MooseClass now provides "my_method" and "meta" only.
This module is inspired by namespace::clean module but it doesn't require compiled XS modules. It also doesn't work lexically so unimport method have to be called explicitly.
unimport
See also: namespace::clean, Class::Inspector.
If you find the bug or want to implement new features, please report it at http://rt.cpan.org/NoAuth/Bugs.html?Dist=namespace-functions
Piotr Roszatycki <dexter@cpan.org>
Copyright (C) 2009, 2011 by Piotr Roszatycki <dexter@cpan.org>.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See http://www.perl.com/perl/misc/Artistic.html
To install namespace::functions, copy and paste the appropriate command in to your terminal.
cpanm
cpanm namespace::functions
CPAN shell
perl -MCPAN -e shell install namespace::functions
For more information on module installation, please visit the detailed CPAN module installation guide.