File Coverage

blib/lib/Importer/Zim/EndOfScope.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::EndOfScope;
3             $Importer::Zim::EndOfScope::VERSION = '0.4.0';
4             # ABSTRACT: Import functions with compilation block scope
5              
6 2     2   55288 use 5.010001;
  2         15  
7              
8 2     2   467 use B::Hooks::EndOfScope ();
  2         15499  
  2         38  
9 2     2   488 use Sub::Replace ();
  2         2264  
  2         51  
10              
11 2     2   466 use Importer::Zim::Utils 0.8.0 qw(DEBUG );
  2         1171  
  2         8  
12              
13             sub import {
14 3     3   1022 require Importer::Zim::Base;
15 3         1382 Importer::Zim::Base->VERSION('0.12.0');
16 3         15 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 3     3   687 my $old = Sub::Replace::sub_replace(@_);
28              
29             # Clean it up after a scope finished compilation
30             B::Hooks::EndOfScope::on_scope_end(
31             sub {
32 3     3   207 warn qq{ Restoring @{[map qq{"$_"}, sort keys %$old]}\n}
33             if DEBUG;
34 3         8 Sub::Replace::sub_replace($old);
35             }
36 3 50       907 ) if %$old;
37             }
38              
39 2     2   519 no Importer::Zim::Utils qw(DEBUG );
  2         4  
  2         7  
40              
41             1;
42              
43             #pod =encoding utf8
44             #pod
45             #pod =head1 SYNOPSIS
46             #pod
47             #pod use Importer::Zim::EndOfScope 'Scalar::Util' => 'blessed';
48             #pod use Importer::Zim::EndOfScope 'Scalar::Util' =>
49             #pod ( 'blessed' => { -as => 'typeof' } );
50             #pod
51             #pod use Importer::Zim::EndOfScope 'Mango::BSON' => ':bson';
52             #pod
53             #pod use Importer::Zim::EndOfScope 'Foo' => { -version => '3.0' } => 'foo';
54             #pod
55             #pod use Importer::Zim::EndOfScope 'SpaceTime::Machine' => [qw(robot rubber_pig)];
56             #pod
57             #pod =head1 DESCRIPTION
58             #pod
59             #pod "Wait a minute! What planet is this?"
60             #pod – Zim
61             #pod
62             #pod This is a backend for L which makes
63             #pod imported symbols available during the compilation of
64             #pod the surrounding scope.
65             #pod
66             #pod Unlike L, it works for perls before 5.18.
67             #pod Unlike L which plays with lexical subs,
68             #pod this meddles with the symbol tables for a (hopefully short)
69             #pod time interval. (This time interval should be even shorter
70             #pod than the one that applies to L.)
71             #pod
72             #pod =head1 HOW IT WORKS
73             #pod
74             #pod The statement
75             #pod
76             #pod use Importer::Zim::EndOfScope 'Foo' => 'foo';
77             #pod
78             #pod works sort of
79             #pod
80             #pod use B::Hooks::EndOfScope;
81             #pod
82             #pod my $_OLD_SUBS;
83             #pod BEGIN {
84             #pod require Foo;
85             #pod $_OLD_SUBS = Sub::Replace::sub_replace('foo' => \&Foo::foo);
86             #pod }
87             #pod
88             #pod on_scope_end {
89             #pod Sub::Replace::sub_replace($_OLD_SUBS);
90             #pod }
91             #pod
92             #pod That means:
93             #pod
94             #pod =over 4
95             #pod
96             #pod =item *
97             #pod
98             #pod Imported subroutines are installed into the caller namespace at compile time.
99             #pod
100             #pod =item *
101             #pod
102             #pod Imported subroutines are cleaned up after perl finished compiling
103             #pod the surrounding scope.
104             #pod
105             #pod =back
106             #pod
107             #pod See L for a few gotchas about why this is not simply done
108             #pod with Perl statements such as
109             #pod
110             #pod *foo = \&Foo::foo;
111             #pod
112             #pod =head1 DEBUGGING
113             #pod
114             #pod You can set the C environment variable
115             #pod for get some diagnostics information printed to C.
116             #pod
117             #pod IMPORTER_ZIM_DEBUG=1
118             #pod
119             #pod =head1 SEE ALSO
120             #pod
121             #pod L
122             #pod
123             #pod L
124             #pod
125             #pod L
126             #pod
127             #pod =cut
128              
129             __END__