4 use warnings FATAL => 'all';
6 our $VERSION = '1.001001'; # 1.1.1
10 my ($package, $file, $line)
11 = $target =~ /[^0-9]/ ? ($target) : caller($target + 2);
12 qq{package $package;\n}
13 . ($file ? "#line $line \"$file\"\n" : '')
17 my ($action, $target) = @_;
18 eval _prelude($target).qq{sub { shift->$action(\@_) }}
19 or die "Failed to build action sub to ${action} for ${target}: $@";
23 my ($class, $target, @args) = @_;
24 _make_action(import => $target)->($class, @args);
27 sub unimport::out_of {
28 my ($class, $target, @args) = @_;
29 _make_action(unimport => $target)->($class, @args);
36 Import::Into - import packages into other packages
40 package My::MultiExporter;
49 Thing1->import::into($target);
50 Thing2->import::into($target, qw(import arguments));
53 Note: you don't need to do anything more clever than this provided you
54 document that people wanting to re-export your module should also be using
55 L<Import::Into>. In fact, for a single module you can simply do:
59 Thing1->import::into(scalar caller);
64 use base qw(Exporter);
67 shift->export_to_level(1);
68 Thing1->import::into(scalar caller);
71 Note 2: You do B<not> need to do anything to Thing1 to be able to call
72 C<import::into> on it. This is a global method, and is callable on any
73 package (and in fact on any object as well, although it's rarer that you'd
76 Finally, we also provide an C<unimport::out_of> to allow the exporting of the
79 # unimport::out_of was added in 1.1.0 (1.001000)
81 Moose->unimport::out_of(scalar caller); # no MyThing == no Moose
84 If how and why this all works is of interest to you, please read on to the
85 description immediately below.
89 Writing exporters is a pain. Some use L<Exporter>, some use L<Sub::Exporter>,
90 some use L<Moose::Exporter>, some use L<Exporter::Declare> ... and some things
93 If you want to re-export other things, you have to know which is which.
94 L<Exporter> subclasses provide export_to_level, but if they overrode their
95 import method all bets are off. L<Sub::Exporter> provides an into parameter
96 but figuring out something used it isn't trivial. Pragmas need to have
97 their C<import> method called directly since they affect the current unit of
102 However, there is an approach that actually works for all of these types.
104 eval "package $target; use $thing;"
106 will work for anything checking caller, which is everything except pragmas.
107 But it doesn't work for pragmas - pragmas need:
111 because they're designed to affect the code currently being compiled - so
112 within an eval, that's the scope of the eval itself, not the module that
113 just C<use>d you - so
119 doesn't do what you wanted, but
125 will apply L<strict> to the calling file correctly.
127 Of course, now you have two new problems - first, that you still need to
128 know if something's a pragma, and second that you can't use either of
129 these approaches alone on something like L<Moose> or L<Moo> that's both
130 an exporter and a pragma.
132 So, the complete solution is:
134 my $sub = eval "package $target; sub { shift->import(\@_) }";
135 $sub->($thing, @import_args);
137 which means that import is called from the right place for pragmas to take
138 effect, and from the right package for caller checking to work - and so
139 behaves correctly for all types of exporter, for pragmas, and for hybrids.
141 Remembering all this, however, is excessively irritating. So I wrote a module
142 so I didn't have to anymore. Loading L<Import::Into> creates a global method
143 C<import::into> which you can call on any package to import it into another
144 package. So now you can simply write:
148 $thing->import::into($target, @import_args);
150 This works because of how perl resolves method calls - a call to a simple
151 method name is resolved against the package of the class or object, so
153 $thing->method_name(@args);
155 is roughly equivalent to:
157 my $code_ref = $thing->can('method_name');
158 $code_ref->($thing, @args);
160 while if a C<::> is found, the lookup is made relative to the package name
161 (i.e. everything before the last C<::>) so
163 $thing->Package::Name::method_name(@args);
165 is roughly equivalent to:
167 my $code_ref = Package::Name->can('method_name');
168 $code_ref->($thing, @args);
170 So since L<Import::Into> defines a method C<into> in package C<import>
171 the syntax reliably calls that.
173 For more craziness of this order, have a look at the article I wrote at
174 L<http://shadow.cat/blog/matt-s-trout/madness-with-methods> which covers
175 coderef abuse and the C<${\...}> syntax.
177 Final note: You do still need to ensure that you already loaded C<$thing> - if
178 you're receiving this from a parameter, I recommend using L<Module::Runtime>:
181 use Module::Runtime qw(use_module);
183 use_module($thing)->import::into($target, @import_args);
187 =head1 ACKNOWLEDGEMENTS
189 Thanks to Getty for asking "how can I get C<< use strict; use warnings; >>
190 turned on for all consumers of my code?" and then "why is this not a
195 mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
199 haarg - Graham Knop (cpan:HAARG) <haarg@haarg.org>
203 Copyright (c) 2012 the Import::Into L</AUTHOR> and L</CONTRIBUTORS>
208 This library is free software and may be distributed under the same terms