summaryrefslogtreecommitdiffstats
path: root/lib/lexically.pm
blob: 8ba368c8542e21c8c2378a94c9d18837df0af4c7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package lexically;
use strict;
use warnings;
# ABSTRACT: lexically import functions from non-lexical exporters

use Exporter::Lexical 0.02 ();
use Module::Runtime 'require_module';

=head1 SYNOPSIS

  package Foo;
  use Moose;
  use lexically 'Scalar::Util' => 'reftype';

=head1 DESCRIPTION

This pragma turns normal package-based exporter modules into lexical exporters.
This can be useful to ensure that your package namespace doesn't get polluted
(preventing the need for something like L<namespace::clean> entirely).

=cut

our $INDEX = 0;

sub import {
    shift;
    my ($package, @args) = @_;

    my $index = $INDEX++;
    my $scratchpad = "lexically::scratchpad_$index";
    my $stash = do {
        no strict 'refs';
        \%{ $scratchpad . '::' }
    };

    require_module($package);

    eval qq[package $scratchpad; '$package'->import(\@args)];
    die if $@;

    my @exports = grep {
        ref(\$stash->{$_}) ne 'GLOB' || defined(*{ $stash->{$_} }{CODE})
    } keys %$stash;

    my $import = Exporter::Lexical::build_exporter({
        -exports => \@exports,
    }, $scratchpad);

    $import->($package, @args);

    delete $lexically::{"scratchpad_${index}::"};
}

=head1 BUGS

No known bugs.

Please report any bugs to GitHub Issues at L<https://github.com/doy/lexically/issues>.

=head1 SEE ALSO

L<Exporter::Lexical>

=head1 SUPPORT

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

    perldoc lexically

You can also look for information at:

=over 4

=item * MetaCPAN

L<https://metacpan.org/release/lexically>

=item * Github

L<https://github.com/doy/lexically>

=item * RT: CPAN's request tracker

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=lexically>

=item * CPAN Ratings

L<http://cpanratings.perl.org/d/lexically>

=back

=cut

1;