summaryrefslogtreecommitdiffstats
path: root/lib/lexically.pm
blob: ee41007f8bfd316e58e3f4903fe750a25c559a27 (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
package lexically;
use strict;
use warnings;

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

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}::"};
}

1;