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