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