File Coverage

blib/lib/Importer/Zim/Unit.pm
Criterion Covered Total %
statement 21 27 77.7
branch 1 6 16.6
condition 0 3 0.0
subroutine 8 9 88.8
pod 0 1 0.0
total 30 46 65.2


line stmt bran cond sub pod time code
1              
2             package Importer::Zim::Unit;
3             $Importer::Zim::Unit::VERSION = '0.5.0';
4             # ABSTRACT: Import functions with compilation unit scope
5              
6 2     2   53301 use 5.010001;
  2         13  
7              
8 2     2   462 use Devel::Hook ();
  2         1326  
  2         38  
9 2     2   532 use Sub::Replace ();
  2         2386  
  2         47  
10              
11 2     2   513 use Importer::Zim::Utils 0.8.0 qw(DEBUG );
  2         1405  
  2         13  
12              
13             sub import {
14 1     1   286 require Importer::Zim::Base;
15 1         3534 Importer::Zim::Base->VERSION('0.12.0');
16 1         6 goto &Importer::Zim::Base::import_into;
17             }
18              
19             sub export_to {
20 0     0 0 0 my $t = shift;
21 0 0 0     0 @_ = %{ $_[0] } if @_ == 1 && ref $_[0] eq 'HASH';
  0         0  
22 0 0       0 @_ = map { $_ & 1 ? $_[$_] : "${t}::$_[$_]" } 0 .. $#_;
  0         0  
23 0         0 goto &_export_to;
24             }
25              
26             sub _export_to {
27 1     1   246 my $old = Sub::Replace::sub_replace(@_);
28              
29             # Clean it up after compilation
30             Devel::Hook->unshift_UNITCHECK_hook(
31             sub {
32 1     1   608 warn qq{ Restoring @{[map qq{"$_"}, sort keys %$old]}\n}
33             if DEBUG;
34 1         5 Sub::Replace::sub_replace($old);
35             }
36 1 50       246 ) if %$old;
37             }
38              
39 2     2   474 no Importer::Zim::Utils qw(DEBUG );
  2         4  
  2         8  
40              
41             1;
42              
43             #pod =encoding utf8
44             #pod
45             #pod =head1 SYNOPSIS
46             #pod
47             #pod use Importer::Zim::Unit 'Scalar::Util' => 'blessed';
48             #pod use Importer::Zim::Unit 'Scalar::Util' =>
49             #pod ( 'blessed' => { -as => 'typeof' } );
50             #pod
51             #pod use Importer::Zim::Unit 'Mango::BSON' => ':bson';
52             #pod
53             #pod use Importer::Zim::Unit 'Foo' => { -version => '3.0' } => 'foo';
54             #pod
55             #pod use Importer::Zim::Unit 'SpaceTime::Machine' => [qw(robot rubber_pig)];
56             #pod
57             #pod =head1 DESCRIPTION
58             #pod
59             #pod "I'm gonna roll around on the floor for a while. KAY?"
60             #pod – GIR
61             #pod
62             #pod This is a backend for L which makes imported
63             #pod symbols available during compilation.
64             #pod
65             #pod Unlike L, it works for perls before 5.18.
66             #pod Unlike L which plays with lexical subs,
67             #pod this meddles with the symbol tables for a (hopefully short)
68             #pod time interval.
69             #pod
70             #pod =head1 HOW IT WORKS
71             #pod
72             #pod The statement
73             #pod
74             #pod use Importer::Zim::Unit 'Foo' => 'foo';
75             #pod
76             #pod works sort of
77             #pod
78             #pod use Sub::Replace;
79             #pod
80             #pod my $_OLD_SUBS;
81             #pod BEGIN {
82             #pod require Foo;
83             #pod $_OLD_SUBS = Sub::Replace::sub_replace('foo' => \&Foo::foo);
84             #pod }
85             #pod
86             #pod UNITCHECK {
87             #pod Sub::Replace::sub_replace($_OLD_SUBS);
88             #pod }
89             #pod
90             #pod That means:
91             #pod
92             #pod =over 4
93             #pod
94             #pod =item *
95             #pod
96             #pod Imported subroutines are installed into the caller namespace at compile time.
97             #pod
98             #pod =item *
99             #pod
100             #pod Imported subroutines are cleaned up just after the unit which defined
101             #pod them has been compiled.
102             #pod
103             #pod =back
104             #pod
105             #pod See L<< perlsub /BEGIN, UNITCHECK, CHECK, INIT and END >> for
106             #pod the concept of "compilation unit" which is relevant here.
107             #pod
108             #pod See L for a few gotchas about why this is not simply done
109             #pod with Perl statements such as
110             #pod
111             #pod *foo = \&Foo::foo;
112             #pod
113             #pod =head1 DEBUGGING
114             #pod
115             #pod You can set the C environment variable
116             #pod for get some diagnostics information printed to C.
117             #pod
118             #pod IMPORTER_ZIM_DEBUG=1
119             #pod
120             #pod =head1 SEE ALSO
121             #pod
122             #pod L
123             #pod
124             #pod L<< perlsub /BEGIN, UNITCHECK, CHECK, INIT and END >>
125             #pod
126             #pod L
127             #pod
128             #pod L
129             #pod
130             #pod =cut
131              
132             __END__