documentationtality
[p5sagit/Import-Into.git] / lib / Import / Into.pm
CommitLineData
2afb5246 1package Import::Into;
2
3use strict;
4use warnings FATAL => 'all';
5
6our $VERSION = '1.0';
7
8my %importers;
9
10sub import::into {
11 my ($class, $target, @args) = @_;
12 $class->${\(
13 $importers{$target} ||= eval qq{
14 package $target;
15 sub { shift->import(\@_) };
16 } or die "Couldn't build importer for $target: $@"
17 )}(@args);
18}
19
201;
21
22=head1 NAME
23
24Import::Into - import packages into other packages
25
26=head1 SYNOPSIS
27
28 package My::MultiExporter;
29
e0ff3439 30 use Import::Into;
31
2afb5246 32 use Thing1 ();
33 use Thing2 ();
34
35 sub import {
36 my $target = caller;
37 Thing1->import::into($target);
38 Thing2->import::into($target, qw(import arguments));
39 }
40
e0ff3439 41=head1 DESCRIPTION
42
43Writing exporters is a pain. Some use L<Exporter>, some use L<Sub::Exporter>,
44some use L<Moose::Exporter>, some use L<Exporter::Declare> ... and some things
45are pragmas.
46
47If you want to re-export other things, you have to know which is which.
48L<Exporter> subclasses provide export_to_level, but if they overrode their
49import method all bets are off. L<Sub::Exporter> provides an into parameter
50but figuring out something used it isn't trivial. Pragmas need to have
51their C<import> method called directly since they affect the current unit of
52compilation.
53
54It's ... annoying.
55
56However, there is an approach that actually works for all of these types.
57
58 eval "package $target; use $thing;"
59
60will work for anything checking caller, which is everything except pragmas.
61But it doesn't work for pragmas - pragmas need:
62
63 $thing->import;
64
65So, the solution is:
66
67 my $sub = eval "package $target; sub { shift->import(\@_) }";
68 $sub->($thing, @import_args);
69
70which means that import is called from the right place for pragmas to take
71effect, and from the right package for caller checking to work.
72
73Remembering all this, however, is excessively irritating. So I wrote a module
74so I didn't have to anymore. Loading L<Import::Into> will create a method
75C<import::into> which you can call on a package to import it into another
76package. So now you can simply write:
77
78 use Import::Into;
79
80 $thing->import::into($target, @import_args);
81
82Just make sure you already loaded C<$thing> - if you're receiving this from
83a parameter, I recommend using L<Module::Runtime>:
84
85 use Import::Into;
86 use Module::Runtime qw(use_module);
87
88 use_module($thing)->import::into($target, @import_args);
89
90And that's it.
91
2afb5246 92=head1 AUTHOR
93
94mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
95
e0ff3439 96=head1 CONTRIBUTORS
97
98None yet - maybe this software is perfect! (ahahahahahahahahaha)
99
2afb5246 100=head1 COPYRIGHT
101
102Copyright (c) 2010-2011 the Import::Into L</AUTHOR> and L</CONTRIBUTORS>
103as listed above.
104
105=head1 LICENSE
106
107This library is free software and may be distributed under the same terms
108as perl itself.