File Coverage

blib/lib/Sub/Inject.pm
Criterion Covered Total %
statement 2 2 100.0
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 3 3 100.0


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