From 5c9420df616f091d317fda0c0fce38359f736da6 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 10 Jul 2013 17:11:20 -0400 Subject: allow just building the exporter without installing it --- lib/Exporter/Lexical.pm | 21 ++++++++++++++------- t/build_exporter.t | 17 +++++++++++++++++ t/lib/Bar.pm | 20 ++++++++++++++++++++ 3 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 t/build_exporter.t create mode 100644 t/lib/Bar.pm diff --git a/lib/Exporter/Lexical.pm b/lib/Exporter/Lexical.pm index 0a1ea5a..633da52 100644 --- a/lib/Exporter/Lexical.pm +++ b/lib/Exporter/Lexical.pm @@ -47,12 +47,24 @@ sub import { my $caller = caller; - my $import = sub { + my $import = build_exporter(\%opts, $caller); + + { + no strict 'refs'; + *{ $caller . '::import' } = $import; + } +} + +sub build_exporter { + my ($opts, $caller) = @_; + $caller //= caller; + + return sub { my $caller_stash = do { no strict 'refs'; \%{ $caller . '::' }; }; - my @exports = @{ $opts{'-exports'} }; + my @exports = @{ $opts->{'-exports'} }; my %exports = map { $_ => \&{ $caller_stash->{$_} } } @exports; for my $export (keys %exports) { @@ -65,11 +77,6 @@ sub import { # for now by injecting a dummy statement right after the 'use'. _lex_stuff(";1;"); }; - - { - no strict 'refs'; - *{ $caller . '::import' } = $import; - } } =head1 BUGS diff --git a/t/build_exporter.t b/t/build_exporter.t new file mode 100644 index 0000000..621e2aa --- /dev/null +++ b/t/build_exporter.t @@ -0,0 +1,17 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Test::More; +use lib 't/lib'; + +sub bar { 'bar' } + +is(bar(), "bar"); +{ + use Bar; + is(bar(), "BAR"); + is($Bar::imported, 1); +} +is(bar(), "bar"); + +done_testing; diff --git a/t/lib/Bar.pm b/t/lib/Bar.pm new file mode 100644 index 0000000..69aadf9 --- /dev/null +++ b/t/lib/Bar.pm @@ -0,0 +1,20 @@ +package Bar; +use strict; +use warnings; + +use Exporter::Lexical (); + +our $imported; + +my $import = Exporter::Lexical::build_exporter({ + -exports => [ qw(bar) ], +}); + +sub import { + $imported = 1; + goto $import; +} + +sub bar { "BAR" } + +1; -- cgit v1.2.3-54-g00ecf