make bump
[p5sagit/Import-Into.git] / lib / Import / Into.pm
CommitLineData
2afb5246 1package Import::Into;
2
3use strict;
4use warnings FATAL => 'all';
cc087979 5use Module::Runtime;
2afb5246 6
f48235db 7our $VERSION = '1.002004';
2afb5246 8
324e7017 9sub _prelude {
10074211 10 my $target = shift;
568eef3e 11 my ($package, $file, $line, $level)
12 = ref $target ? @{$target}{qw(package filename line)}
13 : $target =~ /[^0-9]/ ? ($target)
14 : (undef, undef, undef, $target);
15 if (defined $level) {
16 my ($p, $fn, $ln) = caller($level + 2);
17 $package ||= $p;
18 $file ||= $fn;
19 $line ||= $ln;
20 }
324e7017 21 qq{package $package;\n}
ac6d2081 22 . ($file ? "#line $line \"$file\"\n" : '')
10074211 23}
10074211 24
5f5f09b1 25sub _make_action {
26 my ($action, $target) = @_;
568eef3e 27 my $version = ref $target && $target->{version};
cc087979 28 my $ver_check = $version ? ', $version' : '';
29 eval _prelude($target)
30 . qq{sub { Module::Runtime::use_module( shift$ver_check )->$action(\@_) }}
5f5f09b1 31 or die "Failed to build action sub to ${action} for ${target}: $@";
32}
33
2afb5246 34sub import::into {
35 my ($class, $target, @args) = @_;
5f5f09b1 36 _make_action(import => $target)->($class, @args);
10074211 37}
38
39sub unimport::out_of {
40 my ($class, $target, @args) = @_;
5f5f09b1 41 _make_action(unimport => $target)->($class, @args);
2afb5246 42}
43
441;
74957c98 45
46__END__
47
2afb5246 48=head1 NAME
49
e6231601 50Import::Into - Import packages into other packages
2afb5246 51
52=head1 SYNOPSIS
53
54 package My::MultiExporter;
55
e0ff3439 56 use Import::Into;
57
074eb8db 58 # simple
59 sub import {
60 Thing1->import::into(scalar caller);
61 }
62
63 # multiple
2afb5246 64 sub import {
65 my $target = caller;
66 Thing1->import::into($target);
67 Thing2->import::into($target, qw(import arguments));
68 }
69
074eb8db 70 # by level
8c17b6f8 71 sub import {
074eb8db 72 Thing1->import::into(1);
8c17b6f8 73 }
74
074eb8db 75 # with exporter
8c17b6f8 76 use base qw(Exporter);
8c17b6f8 77 sub import {
78 shift->export_to_level(1);
074eb8db 79 Thing1->import::into(1);
8c17b6f8 80 }
81
074eb8db 82 # no My::MultiExporter == no Thing1
10074211 83 sub unimport {
074eb8db 84 Thing1->unimport::out_of(scalar caller);
10074211 85 }
86
2b421571 87People wanting to re-export your module should also be using L<Import::Into>.
88Any exporter or pragma will work seamlessly.
074eb8db 89
74957c98 90Note: You do B<not> need to make any changes to Thing1 to be able to call
074eb8db 91C<import::into> on it. This is a global method, and is callable on any
92package (and in fact on any object as well, although it's rarer that you'd
93want to do that).
aa5ad642 94
e0ff3439 95=head1 DESCRIPTION
96
97Writing exporters is a pain. Some use L<Exporter>, some use L<Sub::Exporter>,
98some use L<Moose::Exporter>, some use L<Exporter::Declare> ... and some things
99are pragmas.
100
074eb8db 101Exporting on someone else's behalf is harder. The exporters don't provide a
102consistent API for this, and pragmas need to have their import method called
103directly, since they effect the current unit of compilation.
104
105C<Import::Into> provides global methods to make this painless.
106
107=head1 METHODS
108
109=head2 $package->import::into( $target, @arguments );
110
cc087979 111A global method, callable on any package. Loads and imports the given package
112into C<$target>. C<@arguments> are passed along to the package's import method.
074eb8db 113
74957c98 114C<$target> can be an package name to export to, an integer for the
115caller level to export to, or a hashref with the following options:
074eb8db 116
117=over 4
118
119=item package
120
121The target package to export to.
122
123=item filename
124
74957c98 125The apparent filename to export to. Some exporting modules, such as
126L<autodie> or L<strictures>, care about the filename they are being imported
127to.
074eb8db 128
129=item line
130
74957c98 131The apparent line number to export to. To be combined with the C<filename>
132option.
074eb8db 133
134=item level
135
74957c98 136The caller level to export to. This will automatically populate the
137C<package>, C<filename>, and C<line> options, making it the easiest most
138constent option.
074eb8db 139
140=item version
141
74957c98 142A version number to check for the module. The equivalent of specifying the
143version number on a C<use> line.
074eb8db 144
145=back
146
147=head2 $package->unimport::out_of( $target, @arguments );
148
74957c98 149Equivalent to C<import::into>, but dispatches to C<$package>'s C<unimport>
150method instead of C<import>.
074eb8db 151
152=head1 WHY USE THIS MODULE
153
154The APIs for exporting modules aren't consistent. L<Exporter> subclasses
155provide export_to_level, but if they overrode their import method all bets
156are off. L<Sub::Exporter> provides an into parameter but figuring out
157something used it isn't trivial. Pragmas need to have their C<import> method
158called directly since they affect the current unit of compilation.
e0ff3439 159
160It's ... annoying.
161
162However, there is an approach that actually works for all of these types.
163
164 eval "package $target; use $thing;"
165
166will work for anything checking caller, which is everything except pragmas.
167But it doesn't work for pragmas - pragmas need:
168
169 $thing->import;
170
8c17b6f8 171because they're designed to affect the code currently being compiled - so
172within an eval, that's the scope of the eval itself, not the module that
173just C<use>d you - so
174
175 sub import {
176 eval "use strict;"
177 }
178
179doesn't do what you wanted, but
180
181 sub import {
182 strict->import;
183 }
184
185will apply L<strict> to the calling file correctly.
186
187Of course, now you have two new problems - first, that you still need to
188know if something's a pragma, and second that you can't use either of
189these approaches alone on something like L<Moose> or L<Moo> that's both
190an exporter and a pragma.
191
ba1c5bf0 192So, a solution for that is:
e0ff3439 193
cc087979 194 use Module::Runtime;
195 my $sub = eval "package $target; sub { use_module(shift)->import(\@_) }";
e0ff3439 196 $sub->($thing, @import_args);
197
198which means that import is called from the right place for pragmas to take
8c17b6f8 199effect, and from the right package for caller checking to work - and so
200behaves correctly for all types of exporter, for pragmas, and for hybrids.
e0ff3439 201
ba1c5bf0 202Additionally, some import routines check the filename they are being imported
203to. This can be dealt with by generating a L<#line directive|perlsyn/Plain
204Old Comments (Not!)> in the eval, which will change what C<caller> reports for
205the filename when called in the importer. The filename and line number to use
206in the directive then need to be fetched using C<caller>:
207
995d8262 208 my ($target, $file, $line) = caller(1);
ba1c5bf0 209 my $sub = eval qq{
210 package $target;
211 #line $line "$file"
cc087979 212 sub { use_module(shift)->import(\@_) }
ba1c5bf0 213 };
214 $sub->($thing, @import_args);
215
06bd142d 216And you need to switch between these implementations depending on if you are
08c85698 217targeting a specific package, or something in your call stack.
06bd142d 218
e0ff3439 219Remembering all this, however, is excessively irritating. So I wrote a module
aa5ad642 220so I didn't have to anymore. Loading L<Import::Into> creates a global method
221C<import::into> which you can call on any package to import it into another
e0ff3439 222package. So now you can simply write:
223
224 use Import::Into;
225
226 $thing->import::into($target, @import_args);
227
aa5ad642 228This works because of how perl resolves method calls - a call to a simple
229method name is resolved against the package of the class or object, so
230
231 $thing->method_name(@args);
232
233is roughly equivalent to:
234
235 my $code_ref = $thing->can('method_name');
236 $code_ref->($thing, @args);
237
238while if a C<::> is found, the lookup is made relative to the package name
239(i.e. everything before the last C<::>) so
240
241 $thing->Package::Name::method_name(@args);
242
243is roughly equivalent to:
244
245 my $code_ref = Package::Name->can('method_name');
246 $code_ref->($thing, @args);
247
248So since L<Import::Into> defines a method C<into> in package C<import>
249the syntax reliably calls that.
250
251For more craziness of this order, have a look at the article I wrote at
252L<http://shadow.cat/blog/matt-s-trout/madness-with-methods> which covers
253coderef abuse and the C<${\...}> syntax.
254
e0ff3439 255And that's it.
256
9bf478b2 257=head1 SEE ALSO
258
259I gave a lightning talk on this module (and L<curry> and L<Safe::Isa>) at
260L<YAPC::NA 2013|https://www.youtube.com/watch?v=wFXWV2yY7gE&t=46m05s>.
261
95ecfed2 262=head1 ACKNOWLEDGEMENTS
263
264Thanks to Getty for asking "how can I get C<< use strict; use warnings; >>
265turned on for all consumers of my code?" and then "why is this not a
266module?!".
267
2afb5246 268=head1 AUTHOR
269
270mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
271
e0ff3439 272=head1 CONTRIBUTORS
273
93bb9005 274haarg - Graham Knop (cpan:HAARG) <haarg@haarg.org>
e0ff3439 275
cc087979 276Mithaldu - Christian Walde (cpan:MITHALDU) <walde.christian@gmail.com>
277
2afb5246 278=head1 COPYRIGHT
279
aa5ad642 280Copyright (c) 2012 the Import::Into L</AUTHOR> and L</CONTRIBUTORS>
2afb5246 281as listed above.
282
283=head1 LICENSE
284
285This library is free software and may be distributed under the same terms
286as perl itself.
74957c98 287
288=cut