File Coverage

blib/lib/Importer/Zim.pm
Criterion Covered Total %
statement 33 48 68.7
branch 7 16 43.7
condition 1 6 16.6
subroutine 11 14 78.5
pod 1 2 50.0
total 53 86 61.6


line stmt bran cond sub pod time code
1              
2             package Importer::Zim;
3             $Importer::Zim::VERSION = '0.10.1';
4             # ABSTRACT: Import functions without namespace pollution
5              
6 2     2   63456 use 5.010001;
  2         17  
7 2     2   12 use warnings;
  2         3  
  2         54  
8              
9 2     2   890 use Module::Runtime ();
  2         2948  
  2         57  
10              
11 2     2   788 use Importer::Zim::Utils 0.8.0 qw(DEBUG carp croak);
  2         1516  
  2         8  
12              
13             BEGIN {
14 2   50 2   184 my $v = $ENV{IMPORTER_ZIM_BACKEND} || '+Lexical,+EndOfScope,+Unit,+Bogus';
15 2         359 *DEFAULT_BACKEND = sub () {$v};
  0         0  
16             }
17              
18             my %MIN_VERSION = do {
19             my %v = (
20             '+Lexical' => '0.10.0',
21             '+EndOfScope' => '0.5.0',
22             '+Unit' => '0.6.0',
23             '+Bogus' => '0.12.0',
24             );
25             /^\+/ and $v{ _backend_class($_) } = $v{$_} for keys %v;
26             %v;
27             };
28              
29 1 50 0 1 0 5 sub backend { _backend( ref $_[2] eq 'HASH' ? $_[2]{-how} // '' : '' ) }
30              
31 0     0 1 0 sub export_to { goto &{ __PACKAGE__->backend->can('export_to') } }
  0         0  
32              
33             sub import { # Load +Base if import() is called
34 0     0   0 require Importer::Zim::Base;
35 0         0 Importer::Zim::Base->VERSION('0.12.1');
36 2     2   12 no warnings 'redefine';
  2         3  
  2         778  
37 0         0 *import = \&_import;
38 0         0 goto &_import;
39             }
40              
41 10 50   10   57 sub _backend_class { $_[0] =~ s/^\+// ? __PACKAGE__ . '::' . $_[0] : $_[0] }
42              
43             sub _backend {
44 1     1   1 state $BACKEND_FOR;
45 1 50       4 return $BACKEND_FOR->{ $_[0] } if exists $BACKEND_FOR->{ $_[0] };
46 1 50       6 my @how = split ',', length $_[0] ? $_[0] : DEFAULT_BACKEND;
47 1         2 for my $how (@how) {
48 2         4 my $backend = _backend_class($how);
49             my @version
50 2 50       7 = exists $MIN_VERSION{$backend} ? ( $MIN_VERSION{$backend} ) : ();
51 2         2 my $mod = eval { &Module::Runtime::use_module( $backend, @version ) };
  2         7  
52 2         11062 _trace_backend( $mod, $backend, @version ) if DEBUG;
53 2 100       11 return $BACKEND_FOR->{ $_[0] } = $mod if $mod;
54             }
55 0         0 croak qq{Can't load any backend};
56             }
57              
58             sub _import {
59 1     1   3 unshift @_, shift->backend(@_);
60 1         6 goto &Importer::Zim::Base::import_into;
61             }
62              
63             sub _trace_backend {
64 0     0     my ( $mod, $backend, $version ) = @_;
65 0 0         my $rv = $version ? " $version+" : '';
66 0 0         unless ($mod) {
67 0           carp qq{Failed to load "$backend"$rv backend};
68 0           return;
69             }
70 0   0       my $v = $mod->VERSION // 'NA';
71 0           carp qq{Loaded "$backend"$rv ($v) backend};
72             }
73              
74 2     2   15 no Importer::Zim::Utils qw(DEBUG carp croak);
  2         3  
  2         8  
75              
76             1;
77              
78             #pod =encoding utf8
79             #pod
80             #pod =head1 SYNOPSIS
81             #pod
82             #pod use Importer::Zim 'Scalar::Util' => 'blessed';
83             #pod use Importer::Zim 'Scalar::Util' => 'blessed' => { -as => 'typeof' };
84             #pod
85             #pod use Importer::Zim 'Mango::BSON' => ':bson';
86             #pod
87             #pod use Importer::Zim 'Foo' => { -version => '3.0' } => 'foo';
88             #pod
89             #pod use Importer::Zim 'SpaceTime::Machine' => [qw(robot rubber_pig)];
90             #pod
91             #pod =head1 DESCRIPTION
92             #pod
93             #pod "Because, when you create a giant monster of doom,
94             #pod no matter how cute, you have to... you have to... I don't know."
95             #pod – Zim
96             #pod
97             #pod This pragma imports subroutines from other modules in a clean way.
98             #pod "Clean imports" here mean that the imported symbols will
99             #pod be available for compilation and will not pollute
100             #pod the user namespace at runtime.
101             #pod
102             #pod L relies on pluggable backends which give a precise
103             #pod meaning to "clean imports". For example,
104             #pod L uses lexical subs that are bound
105             #pod to the surrounding lexical scope and never touch the target
106             #pod namespace.
107             #pod
108             #pod By default, L looks at package variables
109             #pod C<@EXPORT>, C<@EXPORT_OK> and C<%EXPORT_TAGS> to decide
110             #pod what are exportable subroutines. It tries its best to implement
111             #pod a behavior akin to L without the corresponding namespace pollution.
112             #pod
113             #pod =head1 BACKENDS
114             #pod
115             #pod L will try the following backends in order
116             #pod until one succeeds to load.
117             #pod
118             #pod =over 4
119             #pod
120             #pod =item *
121             #pod
122             #pod L - symbols are imported as lexical subroutines
123             #pod
124             #pod =item *
125             #pod
126             #pod L - symbols are imported to caller namespace
127             #pod while surrounding scope is compiled
128             #pod
129             #pod =item *
130             #pod
131             #pod L - symbols are imported to caller namespace
132             #pod while current unit is compiled
133             #pod
134             #pod =back
135             #pod
136             #pod Read also L.
137             #pod
138             #pod =head1 METHODS
139             #pod
140             #pod =head2 import
141             #pod
142             #pod Importer::Zim->import($class => @imports);
143             #pod Importer::Zim->import($class => \%opts => @imports);
144             #pod
145             #pod =head1 FUNCTIONS
146             #pod
147             #pod =head2 export_to
148             #pod
149             #pod Importer::Zim::export_to($target, %imports);
150             #pod Importer::Zim::export_to($target, \%imports);
151             #pod
152             #pod =head1 DEBUGGING
153             #pod
154             #pod You can set the C environment variable
155             #pod to get some diagnostics information printed to C.
156             #pod
157             #pod IMPORTER_ZIM_DEBUG=1
158             #pod
159             #pod =head1 SEE ALSO
160             #pod
161             #pod L
162             #pod
163             #pod L
164             #pod
165             #pod L and L
166             #pod
167             #pod L
168             #pod
169             #pod =cut
170              
171             __END__