Importer-Zim-Unit

 view release on metacpan or  search on metacpan

lib/Importer/Zim/Unit.pm  view on Meta::CPAN

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    my $t = shift;
    @_ = %{ $_[0] } if @_ == 1 && ref $_[0] eq 'HASH';
    @_ = map { $_ & 1 ? $_[$_] : "${t}::$_[$_]" } 0 .. $#_;
    goto &_export_to;
}
 
sub _export_to {
    my $old = Sub::Replace::sub_replace(@_);
 
    # Clean it up after compilation
    Devel::Hook->unshift_UNITCHECK_hook(
        sub {
            warn qq{  Restoring @{[map qq{"$_"}, sort keys %$old]}\n}
              if DEBUG;
            Sub::Replace::sub_replace($old);
        }
    ) if %$old;
}
 
no Importer::Zim::Utils qw(DEBUG );

lib/Importer/Zim/Unit.pm  view on Meta::CPAN

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#pod works sort of
#pod
#pod     use Sub::Replace;
#pod
#pod     my $_OLD_SUBS;
#pod     BEGIN {
#pod         require Foo;
#pod         $_OLD_SUBS = Sub::Replace::sub_replace('foo' => \&Foo::foo);
#pod     }
#pod
#pod     UNITCHECK {
#pod         Sub::Replace::sub_replace($_OLD_SUBS);
#pod     }
#pod
#pod That means:
#pod
#pod =over 4
#pod
#pod =item *
#pod
#pod Imported subroutines are installed into the caller namespace at compile time.
#pod
#pod =item *
#pod
#pod Imported subroutines are cleaned up just after the unit which defined
#pod them has been compiled.
#pod
#pod =back
#pod
#pod See L<< perlsub /BEGIN, UNITCHECK, CHECK, INIT and END >> for
#pod the concept of "compilation unit" which is relevant here.
#pod
#pod See L<Sub::Replace> for a few gotchas about why this is not simply done
#pod with Perl statements such as
#pod
#pod     *foo = \&Foo::foo;
#pod
#pod =head1 DEBUGGING
#pod
#pod You can set the C<IMPORTER_ZIM_DEBUG> environment variable
#pod for get some diagnostics information printed to C<STDERR>.
#pod
#pod     IMPORTER_ZIM_DEBUG=1
#pod
#pod =head1 SEE ALSO
#pod
#pod L<Importer::Zim>
#pod
#pod L<< perlsub /BEGIN, UNITCHECK, CHECK, INIT and END >>
#pod
#pod L<Importer::Zim::Lexical>
#pod
#pod L<Importer::Zim::EndOfScope>
#pod
#pod =cut
 
__END__
 
=pod

lib/Importer/Zim/Unit.pm  view on Meta::CPAN

177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
works sort of
 
    use Sub::Replace;
 
    my $_OLD_SUBS;
    BEGIN {
        require Foo;
        $_OLD_SUBS = Sub::Replace::sub_replace('foo' => \&Foo::foo);
    }
 
    UNITCHECK {
        Sub::Replace::sub_replace($_OLD_SUBS);
    }
 
That means:
 
=over 4
 
=item *
 
Imported subroutines are installed into the caller namespace at compile time.
 
=item *
 
Imported subroutines are cleaned up just after the unit which defined
them has been compiled.
 
=back
 
See L<< perlsub /BEGIN, UNITCHECK, CHECK, INIT and END >> for
the concept of "compilation unit" which is relevant here.
 
See L<Sub::Replace> for a few gotchas about why this is not simply done
with Perl statements such as
 
    *foo = \&Foo::foo;
 
=head1 DEBUGGING
 
You can set the C<IMPORTER_ZIM_DEBUG> environment variable
for get some diagnostics information printed to C<STDERR>.
 
    IMPORTER_ZIM_DEBUG=1
 
=head1 SEE ALSO
 
L<Importer::Zim>
 
L<< perlsub /BEGIN, UNITCHECK, CHECK, INIT and END >>
 
L<Importer::Zim::Lexical>
 
L<Importer::Zim::EndOfScope>
 
=head1 AUTHOR
 
Adriano Ferreira <ferreira@cpan.org>
 
=head1 COPYRIGHT AND LICENSE

t/02-basic.t  view on Meta::CPAN

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
BEGIN { our @EXPORT_OK = qw(f1 f2 f3); }
 
sub f1 { }
sub f2 { }
sub f3 { }
 
sub f4 { }
 
package main;
 
UNITCHECK {
    ok( !__PACKAGE__->can('f1'), 'f1 is gone (by UNITCHECK time)' );
    ok( !__PACKAGE__->can('f2'), 'f2 is gone (by UNITCHECK time)' );
}
 
use Importer::Zim::Unit 'M1' => qw(f1 f2);
 
ok( defined &f1, 'f1 was imported' );
is( \&f1, \&M1::f1, 'f1 comes from M1' );
 
ok( defined &f2, 'f2 was imported' );
is( \&f2, \&M1::f2, 'f2 comes from M1' );



( run in 0.317 second using v1.01-cache-2.11-cpan-a9ef4e587e4 )