File Coverage

blib/lib/Importer/Zim.pm
Criterion Covered Total %
statement 31 46 67.3
branch 5 12 41.6
condition 2 6 33.3
subroutine 10 13 76.9
pod 1 3 33.3
total 49 80 61.2


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