Commit | Line | Data |
2afb5246 |
1 | package Import::Into; |
2 | |
3 | use strict; |
4 | use warnings FATAL => 'all'; |
5 | |
6 | our $VERSION = '1.0'; |
7 | |
8 | my %importers; |
9 | |
10 | sub import::into { |
11 | my ($class, $target, @args) = @_; |
12 | $class->${\( |
13 | $importers{$target} ||= eval qq{ |
14 | package $target; |
15 | sub { shift->import(\@_) }; |
16 | } or die "Couldn't build importer for $target: $@" |
17 | )}(@args); |
18 | } |
19 | |
20 | 1; |
21 | |
22 | =head1 NAME |
23 | |
24 | Import::Into - import packages into other packages |
25 | |
26 | =head1 SYNOPSIS |
27 | |
28 | package My::MultiExporter; |
29 | |
e0ff3439 |
30 | use Import::Into; |
31 | |
2afb5246 |
32 | use Thing1 (); |
33 | use Thing2 (); |
34 | |
35 | sub import { |
36 | my $target = caller; |
37 | Thing1->import::into($target); |
38 | Thing2->import::into($target, qw(import arguments)); |
39 | } |
40 | |
e0ff3439 |
41 | =head1 DESCRIPTION |
42 | |
43 | Writing exporters is a pain. Some use L<Exporter>, some use L<Sub::Exporter>, |
44 | some use L<Moose::Exporter>, some use L<Exporter::Declare> ... and some things |
45 | are pragmas. |
46 | |
47 | If you want to re-export other things, you have to know which is which. |
48 | L<Exporter> subclasses provide export_to_level, but if they overrode their |
49 | import method all bets are off. L<Sub::Exporter> provides an into parameter |
50 | but figuring out something used it isn't trivial. Pragmas need to have |
51 | their C<import> method called directly since they affect the current unit of |
52 | compilation. |
53 | |
54 | It's ... annoying. |
55 | |
56 | However, there is an approach that actually works for all of these types. |
57 | |
58 | eval "package $target; use $thing;" |
59 | |
60 | will work for anything checking caller, which is everything except pragmas. |
61 | But it doesn't work for pragmas - pragmas need: |
62 | |
63 | $thing->import; |
64 | |
65 | So, the solution is: |
66 | |
67 | my $sub = eval "package $target; sub { shift->import(\@_) }"; |
68 | $sub->($thing, @import_args); |
69 | |
70 | which means that import is called from the right place for pragmas to take |
71 | effect, and from the right package for caller checking to work. |
72 | |
73 | Remembering all this, however, is excessively irritating. So I wrote a module |
74 | so I didn't have to anymore. Loading L<Import::Into> will create a method |
75 | C<import::into> which you can call on a package to import it into another |
76 | package. So now you can simply write: |
77 | |
78 | use Import::Into; |
79 | |
80 | $thing->import::into($target, @import_args); |
81 | |
82 | Just make sure you already loaded C<$thing> - if you're receiving this from |
83 | a parameter, I recommend using L<Module::Runtime>: |
84 | |
85 | use Import::Into; |
86 | use Module::Runtime qw(use_module); |
87 | |
88 | use_module($thing)->import::into($target, @import_args); |
89 | |
90 | And that's it. |
91 | |
2afb5246 |
92 | =head1 AUTHOR |
93 | |
94 | mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk> |
95 | |
e0ff3439 |
96 | =head1 CONTRIBUTORS |
97 | |
98 | None yet - maybe this software is perfect! (ahahahahahahahahaha) |
99 | |
2afb5246 |
100 | =head1 COPYRIGHT |
101 | |
102 | Copyright (c) 2010-2011 the Import::Into L</AUTHOR> and L</CONTRIBUTORS> |
103 | as listed above. |
104 | |
105 | =head1 LICENSE |
106 | |
107 | This library is free software and may be distributed under the same terms |
108 | as perl itself. |