namespace::local - Confine imports or functions to a given scope
This module allows to confine imports or private functions to a given scope.
package My::Module; sub normal_method { # frobnicate; # nope! }; sub method_with_sugar { use namespace::local; use Crazy::Prototyped::DSL qw(do_this do_that frobnicate); do_this; do_that; frobnicate; }; sub another_method { # frobnicate; # nope! };
The calling module's symbol table is saved at the use line and restored upon leaving the block.
use
The subsequent imports will do their job within the block, but will not be available as methods at runtime.
This confines all subsequent imports and functions between the use of namespace::local and the end of scope.
package My::Package; sub normal_sub { # frobnicate() is unknown } sub using_import { use namespace::local -around; use Some::Crazy::DSL qw(frobnicate); frobnicate Foo => 42; } sub no_import { # frobnicate() is unknown }
Hides subsequent imports and functions on end of scope.
This may be used to mask private functions:
package My::Package; use Moo::Role; # This is available everywhere sub public { return private(); }; use namespace::local -below; # This is only available in the current file sub private { return 42; };
Note that this doesn't work for private methods since methods are resolved at runtime.
Hide all functions and exports above the use line.
This emulates namespace::clean, by which this module is clearly inspired.
package My::Module; use POSIX; use Time::HiRes; use Carp; use namespace::local -above; # now define public functions here
Use no namespace::local (unimport) to force end of scope for the latest namespace::local instance in action:
no namespace::local
unimport
package My::Module; use namespace::local; sub private { ... }; no namespace::local; # private not available here, even though the scope didn't end!
No options are currently supported.
EXPERIMENTAL.
Extra options may be passed to namespace::local:
Act on another package instead of the caller. Note that namespace::local is only meant to be used in BEGIN phase.
Exempt symbols mentioned in list (with sigils) from the module's action.
No sigil means a function. Only names made of word characters are supported.
Exempt symbols with names matching the regular expression from the module's action.
Note that sigils are ignored here.
Only affect the listed symbols (with sigils). Rules are the same as for -except.
Only affect symbols with matching names.
All -only and -except options act together, further restricting the set of affected symbols.
-only
-except
The following symbols are not touched by this module, to avoid breaking things:
anything that does not consist of word characters;
$_, @_, $1, $2, ...;
Arrays: @CARP_NOT, @EXPORT, @EXPORT_OK, @ISA;
@CARP_NOT
@EXPORT
@EXPORT_OK
@ISA
Hashes: %OVERLOAD;
%OVERLOAD
Files: DATA, STDERR, STDIN, STDOUT;
DATA
STDERR
STDIN
STDOUT
Functions: AUTOLOAD, DESTROY, import;
AUTOLOAD
DESTROY
import
Scalars: $AUTOLOAD, $a, $b;
$AUTOLOAD
$a
$b
This list is likely incomplete, and may grow in the future.
None.
This module is highly experimental. The following two conditions are guaranteed to hold at least until leaving the beta stage:
All symbols available before the use line will stay so after end of scope
All functions imported from other modules below the use line with names consisting of words and not present in perlvar are not going to be available after end of scope.
The rest is a big grey zone.
Currently the module works by saving and then restoring globs, so variables and filehandles are also reset. This may be changed in the future.
Due to order of callback execution in B::Hooks::EndOfScope, other modules in namespace:: namespace may interact poorly with namespace::local.
namespace::
Up to v.0.07, -around used to be the default mode instead of -below. -around is much more restrictive, in particular, it prevents functions defined below the block from propagating above the block.
-around
-below
This is less of a problem than imported functions leaking upward. No perfect solution has yet been found.
Set PERL_NAMESPACE_LOCAL=debug to see some debugging information upon module load.
PERL_NAMESPACE_LOCAL=debug
A stack of "command" objects is used behind the scenes.
Its interface is not public, see this module's source.
Calling
on this package will create a command object, not a namespace::local instance.
namespace::local
The creation and destruction of command has no effect on the namespaces.
Instead, special prepare and execute methods are called upon import and leaving scope, respectively.
prepare
execute
As of 0.0604, -around hides subroutines defined below its scope end from anything above it. No solution exists so far.
This is experimental module. There certainly are more bugs.
Bug reports, feature requests, suggestions and general feedback welcome at:
https://github.com/dallaylaen/namespace-local-perl/issues
http://rt.cpan.org/NoAuth/ReportBug.html?Queue=namespace-local
bug-namespace-local at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc
perldoc namespace::local
You can also look for information at:
github:
https://github.com/dallaylaen/namespace-local-perl
RT: CPAN's request tracker (report bugs here)
http://rt.cpan.org/NoAuth/Bugs.html?Dist=namespace-local
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/namespace-local
CPAN Ratings
http://cpanratings.perl.org/d/namespace-local
Search CPAN
http://search.cpan.org/dist/namespace-local/
namespace::clean gave the inspiration for this module.
namespace::sweep, namespace::autoclean and probably more also clean caller's namespace, but differently.
B::Hooks::EndOfScope is used as a backend.
"Symbol Tables" in perlmod explains how reading/writing the namespace works.
Copyright 2018 Konstantin S. Uvarin, <khedin at gmail.com>
<khedin at gmail.com>
This program is free software; you can redistribute it and/or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at:
http://www.perlfoundation.org/artistic_license_2_0
Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License. By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license.
If your Modified Version has been derived from a Modified Version made by someone other than you, you are nevertheless required to ensure that your Modified Version complies with the requirements of this license.
This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder.
This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement, then this Artistic License to you shall terminate on the date that such litigation is filed.
Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
To install namespace::local, copy and paste the appropriate command in to your terminal.
cpanm
cpanm namespace::local
CPAN shell
perl -MCPAN -e shell install namespace::local
For more information on module installation, please visit the detailed CPAN module installation guide.