| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
package Sub::Inject; |
|
3
|
|
|
|
|
|
|
$Sub::Inject::VERSION = '0.2_1'; # TRIAL |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
$Sub::Inject::VERSION = '0.21';# ABSTRACT: Inject subroutines into a lexical scope |
|
6
|
|
|
|
|
|
|
|
|
7
|
3
|
|
|
3
|
|
131423
|
use 5.018; |
|
|
3
|
|
|
|
|
9
|
|
|
8
|
|
|
|
|
|
|
|
|
9
|
3
|
|
|
3
|
|
17
|
use XSLoader 0.20; |
|
|
3
|
|
|
|
|
79
|
|
|
|
3
|
|
|
|
|
162
|
|
|
10
|
|
|
|
|
|
|
XSLoader::load(__PACKAGE__); |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
1; |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
#pod =encoding utf8 |
|
15
|
|
|
|
|
|
|
#pod |
|
16
|
|
|
|
|
|
|
#pod =head1 SYNOPSIS |
|
17
|
|
|
|
|
|
|
#pod |
|
18
|
|
|
|
|
|
|
#pod use Sub::Inject; # requires perl 5.18+ |
|
19
|
|
|
|
|
|
|
#pod |
|
20
|
|
|
|
|
|
|
#pod { |
|
21
|
|
|
|
|
|
|
#pod BEGIN { Sub::Inject::sub_inject( 'one', sub { say "One!" } ); } |
|
22
|
|
|
|
|
|
|
#pod one(); |
|
23
|
|
|
|
|
|
|
#pod } |
|
24
|
|
|
|
|
|
|
#pod |
|
25
|
|
|
|
|
|
|
#pod one(); # throws "Undefined subroutine &main::one called" |
|
26
|
|
|
|
|
|
|
#pod |
|
27
|
|
|
|
|
|
|
#pod =head1 DESCRIPTION |
|
28
|
|
|
|
|
|
|
#pod |
|
29
|
|
|
|
|
|
|
#pod This module allows to dynamically inject lexical subs |
|
30
|
|
|
|
|
|
|
#pod during compilation. It is implemented using |
|
31
|
|
|
|
|
|
|
#pod lexical subroutines introduced in perl 5.18. |
|
32
|
|
|
|
|
|
|
#pod |
|
33
|
|
|
|
|
|
|
#pod This is a low level library. It is meant for cases where |
|
34
|
|
|
|
|
|
|
#pod subroutine names and bodies are to be treated as data |
|
35
|
|
|
|
|
|
|
#pod or not known in advance. Otherwise, lexical subs syntax |
|
36
|
|
|
|
|
|
|
#pod is recommended. For instance, |
|
37
|
|
|
|
|
|
|
#pod |
|
38
|
|
|
|
|
|
|
#pod use experimental qw(lexical_subs); |
|
39
|
|
|
|
|
|
|
#pod state sub foo { say "One!" } |
|
40
|
|
|
|
|
|
|
#pod |
|
41
|
|
|
|
|
|
|
#pod is the static equivalent of |
|
42
|
|
|
|
|
|
|
#pod |
|
43
|
|
|
|
|
|
|
#pod BEGIN { |
|
44
|
|
|
|
|
|
|
#pod Sub::Inject::sub_inject( 'one', sub { say "One!" } ); |
|
45
|
|
|
|
|
|
|
#pod } |
|
46
|
|
|
|
|
|
|
#pod |
|
47
|
|
|
|
|
|
|
#pod =head1 HOW IT WORKS |
|
48
|
|
|
|
|
|
|
#pod |
|
49
|
|
|
|
|
|
|
#pod Used like |
|
50
|
|
|
|
|
|
|
#pod |
|
51
|
|
|
|
|
|
|
#pod BEGIN { Sub::Inject::sub_inject('foo', sub { ... }) } |
|
52
|
|
|
|
|
|
|
#pod |
|
53
|
|
|
|
|
|
|
#pod it works as |
|
54
|
|
|
|
|
|
|
#pod |
|
55
|
|
|
|
|
|
|
#pod \state &foo = sub { ... }; |
|
56
|
|
|
|
|
|
|
#pod |
|
57
|
|
|
|
|
|
|
#pod That means: |
|
58
|
|
|
|
|
|
|
#pod |
|
59
|
|
|
|
|
|
|
#pod =over 4 |
|
60
|
|
|
|
|
|
|
#pod |
|
61
|
|
|
|
|
|
|
#pod =item * |
|
62
|
|
|
|
|
|
|
#pod |
|
63
|
|
|
|
|
|
|
#pod The scope behavior is the same as the lexical sub statement |
|
64
|
|
|
|
|
|
|
#pod |
|
65
|
|
|
|
|
|
|
#pod =item * |
|
66
|
|
|
|
|
|
|
#pod |
|
67
|
|
|
|
|
|
|
#pod Being a "state" lexical guarantees the persistence |
|
68
|
|
|
|
|
|
|
#pod of the association between the name and the subroutine |
|
69
|
|
|
|
|
|
|
#pod |
|
70
|
|
|
|
|
|
|
#pod =item * |
|
71
|
|
|
|
|
|
|
#pod |
|
72
|
|
|
|
|
|
|
#pod The reference aliasing operation means no copy is done |
|
73
|
|
|
|
|
|
|
#pod |
|
74
|
|
|
|
|
|
|
#pod =back |
|
75
|
|
|
|
|
|
|
#pod |
|
76
|
|
|
|
|
|
|
#pod =head1 FUNCTIONS |
|
77
|
|
|
|
|
|
|
#pod |
|
78
|
|
|
|
|
|
|
#pod =head2 sub_inject |
|
79
|
|
|
|
|
|
|
#pod |
|
80
|
|
|
|
|
|
|
#pod sub_inject($name, $code); |
|
81
|
|
|
|
|
|
|
#pod sub_inject($name1, $code1, $name2, $code2); |
|
82
|
|
|
|
|
|
|
#pod |
|
83
|
|
|
|
|
|
|
#pod Injects C<$code> as a lexical subroutine named C<$name> |
|
84
|
|
|
|
|
|
|
#pod into the currently compiling scope. The same applies |
|
85
|
|
|
|
|
|
|
#pod to multiple name / code pairs given as input. |
|
86
|
|
|
|
|
|
|
#pod |
|
87
|
|
|
|
|
|
|
#pod Throws an error if called at runtime. |
|
88
|
|
|
|
|
|
|
#pod |
|
89
|
|
|
|
|
|
|
#pod =head1 ACKNOWLEDGEMENTS |
|
90
|
|
|
|
|
|
|
#pod |
|
91
|
|
|
|
|
|
|
#pod This code is a fork of "Lexical.xs" file from |
|
92
|
|
|
|
|
|
|
#pod L |
|
93
|
|
|
|
|
|
|
#pod by L. |
|
94
|
|
|
|
|
|
|
#pod |
|
95
|
|
|
|
|
|
|
#pod =head1 SEE ALSO |
|
96
|
|
|
|
|
|
|
#pod |
|
97
|
|
|
|
|
|
|
#pod L |
|
98
|
|
|
|
|
|
|
#pod |
|
99
|
|
|
|
|
|
|
#pod L |
|
100
|
|
|
|
|
|
|
#pod |
|
101
|
|
|
|
|
|
|
#pod L and L |
|
102
|
|
|
|
|
|
|
#pod |
|
103
|
|
|
|
|
|
|
#pod =cut |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
__END__ |