From a78111e4a8945819186ab6182db924c2935a3930 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 10 Jul 2013 19:46:05 -0400 Subject: initial implementation --- lib/lexically.pm | 40 ++++++++++++++++++++++++++++++++++++++++ t/basic.t | 20 ++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 t/basic.t diff --git a/lib/lexically.pm b/lib/lexically.pm index e69de29..40b796d 100644 --- a/lib/lexically.pm +++ b/lib/lexically.pm @@ -0,0 +1,40 @@ +package lexically; +use strict; +use warnings; + +use Exporter (); +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; diff --git a/t/basic.t b/t/basic.t new file mode 100644 index 0000000..94d9a07 --- /dev/null +++ b/t/basic.t @@ -0,0 +1,20 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Test::More; + +undef $@; +eval "blessed('')"; +like($@, qr/^Undefined subroutine &main::blessed called/); + +{ + use lexically 'Scalar::Util', 'blessed'; + ok(blessed(bless {})); + ok(!blessed([])); +} + +undef $@; +eval "blessed('')"; +like($@, qr/^Undefined subroutine &main::blessed called/); + +done_testing; -- cgit v1.2.3