File Coverage

blib/lib/Sub/Inject.pm
Criterion Covered Total %
statement 4 5 80.0
branch 1 2 50.0
condition 1 3 33.3
subroutine 2 2 100.0
pod 1 1 100.0
total 9 13 69.2


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