4 use warnings FATAL => 'all';
6 our $VERSION = '1.000003'; # 1.0.3
11 my ($class, $target, @args) = @_;
13 $importers{$target} ||= eval qq{
15 sub { shift->import(\@_) };
16 } or die "Couldn't build importer for $target: $@"
24 Import::Into - import packages into other packages
28 package My::MultiExporter;
37 Thing1->import::into($target);
38 Thing2->import::into($target, qw(import arguments));
41 Note: you don't need to do anything more clever than this provided you
42 document that people wanting to re-export your module should also be using
43 L<Import::Into>. In fact, for a single module you can simply do:
47 Thing1->import::into(scalar caller);
52 use base qw(Exporter);
55 shift->export_to_level(1);
56 Thing1->import::into(scalar caller);
59 Note 2: You do B<not> need to do anything to Thing1 to be able to call
60 C<import::into> on it. This is a global method, and is callable on any
61 package (and in fact on any object as well, although it's rarer that you'd
64 If how and why this all works is of interest to you, please read on to the
65 description immediately below.
69 Writing exporters is a pain. Some use L<Exporter>, some use L<Sub::Exporter>,
70 some use L<Moose::Exporter>, some use L<Exporter::Declare> ... and some things
73 If you want to re-export other things, you have to know which is which.
74 L<Exporter> subclasses provide export_to_level, but if they overrode their
75 import method all bets are off. L<Sub::Exporter> provides an into parameter
76 but figuring out something used it isn't trivial. Pragmas need to have
77 their C<import> method called directly since they affect the current unit of
82 However, there is an approach that actually works for all of these types.
84 eval "package $target; use $thing;"
86 will work for anything checking caller, which is everything except pragmas.
87 But it doesn't work for pragmas - pragmas need:
91 because they're designed to affect the code currently being compiled - so
92 within an eval, that's the scope of the eval itself, not the module that
99 doesn't do what you wanted, but
105 will apply L<strict> to the calling file correctly.
107 Of course, now you have two new problems - first, that you still need to
108 know if something's a pragma, and second that you can't use either of
109 these approaches alone on something like L<Moose> or L<Moo> that's both
110 an exporter and a pragma.
112 So, the complete solution is:
114 my $sub = eval "package $target; sub { shift->import(\@_) }";
115 $sub->($thing, @import_args);
117 which means that import is called from the right place for pragmas to take
118 effect, and from the right package for caller checking to work - and so
119 behaves correctly for all types of exporter, for pragmas, and for hybrids.
121 Remembering all this, however, is excessively irritating. So I wrote a module
122 so I didn't have to anymore. Loading L<Import::Into> creates a global method
123 C<import::into> which you can call on any package to import it into another
124 package. So now you can simply write:
128 $thing->import::into($target, @import_args);
130 This works because of how perl resolves method calls - a call to a simple
131 method name is resolved against the package of the class or object, so
133 $thing->method_name(@args);
135 is roughly equivalent to:
137 my $code_ref = $thing->can('method_name');
138 $code_ref->($thing, @args);
140 while if a C<::> is found, the lookup is made relative to the package name
141 (i.e. everything before the last C<::>) so
143 $thing->Package::Name::method_name(@args);
145 is roughly equivalent to:
147 my $code_ref = Package::Name->can('method_name');
148 $code_ref->($thing, @args);
150 So since L<Import::Into> defines a method C<into> in package C<import>
151 the syntax reliably calls that.
153 For more craziness of this order, have a look at the article I wrote at
154 L<http://shadow.cat/blog/matt-s-trout/madness-with-methods> which covers
155 coderef abuse and the C<${\...}> syntax.
157 Final note: You do still need to ensure that you already loaded C<$thing> - if
158 you're receiving this from a parameter, I recommend using L<Module::Runtime>:
161 use Module::Runtime qw(use_module);
163 use_module($thing)->import::into($target, @import_args);
169 mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
173 None yet - maybe this software is perfect! (ahahahahahahahahaha)
177 Copyright (c) 2012 the Import::Into L</AUTHOR> and L</CONTRIBUTORS>
182 This library is free software and may be distributed under the same terms