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. The following modes of operation exist:

-around (the default)

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;
        use Some::Crazy::DSL qw(frobnicate);
        frobnicate Foo => 42;
    }

    sub no_import {
        # frobnicate() is unknown
    }

-below

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

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.

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;

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

  • Files: DATA, STDERR, STDIN, STDOUT;

  • Functions: AUTOLOAD, DESTROY, import;

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

METHOD/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.

However, at least -above and -below switches work as expected if used simultaneously.

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, namespace::sweep, namespace::autoclean...

B::Hooks::EndOfScope is used as a backend.

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.