blib/lib/Importer/Zim/EndOfScope.pm

filter: Statementstmt 82.8% 29 / 35 Branchbran 16.6% 1 / 6 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 90.0% 9 / 10 Podpod 0.0% 0 / 1 totaltotal 68.4% 39 / 57 CC 5 SCAR 18.8
CC
5
Cov
68%
export_to
20.0
_export_to
2.0
BEGIN
1.0
CRAP 6.6
↑ Summary Build »
1  
2 package Importer::Zim::EndOfScope;
3 2 $Importer::Zim::EndOfScope::VERSION = '0.3.0';
4 # ABSTRACT: Import functions with compilation block scope
5  
6 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 BEGIN {
9 2 ▶ require Importer::Zim::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 Importer::Zim::Base->VERSION('0.8.0');
11 2 our @ISA = qw(Importer::Zim::Base);
12 }
13  
14 2 ▶use B::Hooks::EndOfScope ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Sub::Replace ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use Importer::Zim::Utils 0.8.0 qw(DEBUG carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 sub import {
20 3 ▶ my $class = shift;
Subroutine import: called CC=1 SCAR=0.0
21  
22 3 carp "$class->import(@_)" if DEBUG;
23 3 my @exports = $class->_prepare_args(@_);
24  
25 3 my $caller = caller;
26 3 return _export_to( map { ; "${caller}::$_->{export}" => $_->{code} }
27 @exports );
28 }
29  
30 sub export_to {
31 0 ▶ my $t = shift;
Subroutine export_to: not called CC=4 SCAR=30.0
Pod undocumented
32 0 ▶ @_ = %{ $_[0] } if @_ == 1 && ref $_[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and ref $_[0] eq "HASH" 0 0 0
Condition0%0/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table0%0/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
33 0 ▶ @_ = map { $_ & 1 ? $_[$_] : "${t}::$_[$_]" } 0 .. $#_;
Branch: 1 branch
BranchTrueFalseTotal
$_ & 1 ? : 0 0 0
34 0 goto &_export_to;
35 }
36  
37 sub _export_to {
38 3 ▶ my $old = Sub::Replace::sub_replace(@_);
Subroutine _export_to: called CC=2 SCAR=7.0
39  
40 # Clean it up after a scope finished compilation
41 B::Hooks::EndOfScope::on_scope_end(
42 sub {
43 3 ▶ warn qq{ Restoring @{[map qq{"$_"}, sort keys %$old]}\n}
Subroutine __ANON__: called CC=1 SCAR=0.0
44 if DEBUG;
45 3 Sub::Replace::sub_replace($old);
46 }
47 3 ▶ ) if %$old;
Branch: 1 branch
BranchTrueFalseTotal
if %$old 3 0 3
48 }
49  
50 2 ▶no Importer::Zim::Utils qw(DEBUG carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 2 1;
53  
54 #pod =encoding utf8
55 #pod
56 #pod =head1 SYNOPSIS
57 #pod
58 #pod use Importer::Zim::EndOfScope 'Scalar::Util' => 'blessed';
59 #pod use Importer::Zim::EndOfScope 'Scalar::Util' =>
60 #pod ( 'blessed' => { -as => 'typeof' } );
61 #pod
62 #pod use Importer::Zim::EndOfScope 'Mango::BSON' => ':bson';
63 #pod
64 #pod use Importer::Zim::EndOfScope 'Foo' => { -version => '3.0' } => 'foo';
65 #pod
66 #pod use Importer::Zim::EndOfScope 'SpaceTime::Machine' => [qw(robot rubber_pig)];
67 #pod
68 #pod =head1 DESCRIPTION
69 #pod
70 #pod "Wait a minute! What planet is this?"
71 #pod – Zim
72 #pod
73 #pod This is a backend for L<Importer::Zim> which makes
74 #pod imported symbols available during the compilation of
75 #pod the surrounding scope.
76 #pod
77 #pod Unlike L<Importer::Zim::Lexical>, it works for perls before 5.18.
78 #pod Unlike L<Importer::Zim::Lexical> which plays with lexical subs,
79 #pod this meddles with the symbol tables for a (hopefully short)
80 #pod time interval. (This time interval should be even shorter
81 #pod than the one that applies to L<Importer::Zim::Unit>.)
82 #pod
83 #pod =head1 HOW IT WORKS
84 #pod
85 #pod The statement
86 #pod
87 #pod use Importer::Zim::EndOfScope 'Foo' => 'foo';
88 #pod
89 #pod works sort of
90 #pod
91 #pod use B::Hooks::EndOfScope;
92 #pod
93 #pod my $_OLD_SUBS;
94 #pod BEGIN {
95 #pod require Foo;
96 #pod $_OLD_SUBS = Sub::Replace::sub_replace('foo' => \&Foo::foo);
97 #pod }
98 #pod
99 #pod on_scope_end {
100 #pod Sub::Replace::sub_replace($_OLD_SUBS);
101 #pod }
102 #pod
103 #pod That means:
104 #pod
105 #pod =over 4
106 #pod
107 #pod =item *
108 #pod
109 #pod Imported subroutines are installed into the caller namespace at compile time.
110 #pod
111 #pod =item *
112 #pod
113 #pod Imported subroutines are cleaned up after perl finished compiling
114 #pod the surrounding scope.
115 #pod
116 #pod =back
117 #pod
118 #pod See L<Sub::Replace> for a few gotchas about why this is not simply done
119 #pod with Perl statements such as
120 #pod
121 #pod *foo = \&Foo::foo;
122 #pod
123 #pod =head1 DEBUGGING
124 #pod
125 #pod You can set the C<IMPORTER_ZIM_DEBUG> environment variable
126 #pod for get some diagnostics information printed to C<STDERR>.
127 #pod
128 #pod IMPORTER_ZIM_DEBUG=1
129 #pod
130 #pod =head1 SEE ALSO
131 #pod
132 #pod L<Importer::Zim>
133 #pod
134 #pod L<B::Hooks::EndOfScope>
135 #pod
136 #pod L<Importer::Zim::Lexical>
137 #pod
138 #pod =cut
139  
140 __END__
↑ Summary Build »