Release commit for 1.000002
[p5sagit/Import-Into.git] / lib / Import / Into.pm
CommitLineData
2afb5246 1package Import::Into;
2
3use strict;
4use warnings FATAL => 'all';
5
2e89909b 6our $VERSION = '1.000002'; # 1.0.2
2afb5246 7
8my %importers;
9
10sub 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
201;
21
22=head1 NAME
23
24Import::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
8c17b6f8 41Note: you don't need to do anything more clever than this provided you
42document that people wanting to re-export your module should also be using
43L<Import::Into>. In fact, for a single module you can simply do:
44
45 sub import {
46 ...
47 Thing1->import::into(scalar caller);
48 }
49
50Notably, this works:
51
52 use base qw(Exporter);
53
54 sub import {
55 shift->export_to_level(1);
56 Thing1->import::into(scalar caller);
57 }
58
e0ff3439 59=head1 DESCRIPTION
60
61Writing exporters is a pain. Some use L<Exporter>, some use L<Sub::Exporter>,
62some use L<Moose::Exporter>, some use L<Exporter::Declare> ... and some things
63are pragmas.
64
65If you want to re-export other things, you have to know which is which.
66L<Exporter> subclasses provide export_to_level, but if they overrode their
67import method all bets are off. L<Sub::Exporter> provides an into parameter
68but figuring out something used it isn't trivial. Pragmas need to have
69their C<import> method called directly since they affect the current unit of
70compilation.
71
72It's ... annoying.
73
74However, there is an approach that actually works for all of these types.
75
76 eval "package $target; use $thing;"
77
78will work for anything checking caller, which is everything except pragmas.
79But it doesn't work for pragmas - pragmas need:
80
81 $thing->import;
82
8c17b6f8 83because they're designed to affect the code currently being compiled - so
84within an eval, that's the scope of the eval itself, not the module that
85just C<use>d you - so
86
87 sub import {
88 eval "use strict;"
89 }
90
91doesn't do what you wanted, but
92
93 sub import {
94 strict->import;
95 }
96
97will apply L<strict> to the calling file correctly.
98
99Of course, now you have two new problems - first, that you still need to
100know if something's a pragma, and second that you can't use either of
101these approaches alone on something like L<Moose> or L<Moo> that's both
102an exporter and a pragma.
103
104So, the complete solution is:
e0ff3439 105
106 my $sub = eval "package $target; sub { shift->import(\@_) }";
107 $sub->($thing, @import_args);
108
109which means that import is called from the right place for pragmas to take
8c17b6f8 110effect, and from the right package for caller checking to work - and so
111behaves correctly for all types of exporter, for pragmas, and for hybrids.
e0ff3439 112
113Remembering all this, however, is excessively irritating. So I wrote a module
114so I didn't have to anymore. Loading L<Import::Into> will create a method
115C<import::into> which you can call on a package to import it into another
116package. So now you can simply write:
117
118 use Import::Into;
119
120 $thing->import::into($target, @import_args);
121
122Just make sure you already loaded C<$thing> - if you're receiving this from
123a parameter, I recommend using L<Module::Runtime>:
124
125 use Import::Into;
126 use Module::Runtime qw(use_module);
127
128 use_module($thing)->import::into($target, @import_args);
129
130And that's it.
131
2afb5246 132=head1 AUTHOR
133
134mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
135
e0ff3439 136=head1 CONTRIBUTORS
137
138None yet - maybe this software is perfect! (ahahahahahahahahaha)
139
2afb5246 140=head1 COPYRIGHT
141
142Copyright (c) 2010-2011 the Import::Into L</AUTHOR> and L</CONTRIBUTORS>
143as listed above.
144
145=head1 LICENSE
146
147This library is free software and may be distributed under the same terms
148as perl itself.