The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

namespace::local - Confine imports or functions to a given scope

SYNOPSIS

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.

The subsequent imports will do their job within the block, but will not be available as methods at runtime.

MODES OF OPERATION

-around

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
    }

-below (the default)

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.

-above

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

no namespace::local

Use no namespace::local (unimport) to force end of scope for the latest namespace::local instance in action:

    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.

OPTIONS

Extra options may be passed to namespace::local:

-target => Package::Name

Act on another package instead of the caller. Note that namespace::local is only meant to be used in BEGIN phase.

-except => \@list

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.

-except => <regex>

Exempt symbols with names matching the regular expression from the module's action.

Note that sigils are ignored here.

-only => \@list

Only affect the listed symbols (with sigils). Rules are the same as for -except.

-only => <regex>

Only affect symbols with matching names.

All -only and -except options act together, further restricting the set of affected symbols.

EXEMPTIONS

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;

  • Hashes: %OVERLOAD;

  • Files: DATA, STDERR, STDIN, STDOUT;

  • Functions: AUTOLOAD, DESTROY, import;

  • Scalars: $AUTOLOAD, $a, $b;

This list is likely incomplete, and may grow in the future.

METHODS/FUNCTIONS

None.

CAVEATS

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.

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.

This is less of a problem than imported functions leaking upward. No perfect solution has yet been found.

ENVIRONMENT

Set PERL_NAMESPACE_LOCAL=debug to see some debugging information upon module load.

THE INTERNALS

A stack of "command" objects is used behind the scenes.

Its interface is not public, see this module's source.

Calling

new

on this package will create a command object, not a namespace::local instance.

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.

BUGS

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:

SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc namespace::local

You can also look for information at:

SEE ALSO

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.

LICENSE AND COPYRIGHT

Copyright 2018 Konstantin S. Uvarin, <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.