4 use warnings FATAL => 'all';
6 our $VERSION = '1.000002'; # 1.0.2
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);
61 Writing exporters is a pain. Some use L<Exporter>, some use L<Sub::Exporter>,
62 some use L<Moose::Exporter>, some use L<Exporter::Declare> ... and some things
65 If you want to re-export other things, you have to know which is which.
66 L<Exporter> subclasses provide export_to_level, but if they overrode their
67 import method all bets are off. L<Sub::Exporter> provides an into parameter
68 but figuring out something used it isn't trivial. Pragmas need to have
69 their C<import> method called directly since they affect the current unit of
74 However, there is an approach that actually works for all of these types.
76 eval "package $target; use $thing;"
78 will work for anything checking caller, which is everything except pragmas.
79 But it doesn't work for pragmas - pragmas need:
83 because they're designed to affect the code currently being compiled - so
84 within an eval, that's the scope of the eval itself, not the module that
91 doesn't do what you wanted, but
97 will apply L<strict> to the calling file correctly.
99 Of course, now you have two new problems - first, that you still need to
100 know if something's a pragma, and second that you can't use either of
101 these approaches alone on something like L<Moose> or L<Moo> that's both
102 an exporter and a pragma.
104 So, the complete solution is:
106 my $sub = eval "package $target; sub { shift->import(\@_) }";
107 $sub->($thing, @import_args);
109 which means that import is called from the right place for pragmas to take
110 effect, and from the right package for caller checking to work - and so
111 behaves correctly for all types of exporter, for pragmas, and for hybrids.
113 Remembering all this, however, is excessively irritating. So I wrote a module
114 so I didn't have to anymore. Loading L<Import::Into> will create a method
115 C<import::into> which you can call on a package to import it into another
116 package. So now you can simply write:
120 $thing->import::into($target, @import_args);
122 Just make sure you already loaded C<$thing> - if you're receiving this from
123 a parameter, I recommend using L<Module::Runtime>:
126 use Module::Runtime qw(use_module);
128 use_module($thing)->import::into($target, @import_args);
134 mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
138 None yet - maybe this software is perfect! (ahahahahahahahahaha)
142 Copyright (c) 2010-2011 the Import::Into L</AUTHOR> and L</CONTRIBUTORS>
147 This library is free software and may be distributed under the same terms