4 use warnings FATAL => 'all';
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));
43 Writing exporters is a pain. Some use L<Exporter>, some use L<Sub::Exporter>,
44 some use L<Moose::Exporter>, some use L<Exporter::Declare> ... and some things
47 If you want to re-export other things, you have to know which is which.
48 L<Exporter> subclasses provide export_to_level, but if they overrode their
49 import method all bets are off. L<Sub::Exporter> provides an into parameter
50 but figuring out something used it isn't trivial. Pragmas need to have
51 their C<import> method called directly since they affect the current unit of
56 However, there is an approach that actually works for all of these types.
58 eval "package $target; use $thing;"
60 will work for anything checking caller, which is everything except pragmas.
61 But it doesn't work for pragmas - pragmas need:
67 my $sub = eval "package $target; sub { shift->import(\@_) }";
68 $sub->($thing, @import_args);
70 which means that import is called from the right place for pragmas to take
71 effect, and from the right package for caller checking to work.
73 Remembering all this, however, is excessively irritating. So I wrote a module
74 so I didn't have to anymore. Loading L<Import::Into> will create a method
75 C<import::into> which you can call on a package to import it into another
76 package. So now you can simply write:
80 $thing->import::into($target, @import_args);
82 Just make sure you already loaded C<$thing> - if you're receiving this from
83 a parameter, I recommend using L<Module::Runtime>:
86 use Module::Runtime qw(use_module);
88 use_module($thing)->import::into($target, @import_args);
94 mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
98 None yet - maybe this software is perfect! (ahahahahahahahahaha)
102 Copyright (c) 2010-2011 the Import::Into L</AUTHOR> and L</CONTRIBUTORS>
107 This library is free software and may be distributed under the same terms