File Coverage

blib/lib/Sub/Mutate.pm
Criterion Covered Total %
statement 12 12 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 16 16 100.0


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Sub::Mutate - examination and modification of subroutines
4              
5             =head1 SYNOPSIS
6              
7             use Sub::Mutate qw(
8             sub_body_type
9             sub_closure_role
10             sub_is_lvalue
11             sub_is_constant
12             sub_is_method mutate_sub_is_method
13             sub_is_debuggable mutate_sub_is_debuggable
14             sub_prototype mutate_sub_prototype
15             );
16              
17             $type = sub_body_type($sub);
18             $type = sub_closure_role($sub);
19             if(sub_is_lvalue($sub)) { ...
20             if(sub_is_constant($sub)) { ...
21             if(sub_is_method($sub)) { ...
22             mutate_sub_is_method($sub, 1);
23             if(sub_is_debuggable($sub)) { ...
24             mutate_sub_is_debuggable($sub, 0);
25             $proto = sub_prototype($sub);
26             mutate_sub_prototype($sub, $proto);
27              
28             use Sub::Mutate qw(when_sub_bodied);
29              
30             when_sub_bodied($sub, sub { mutate_sub_foo($_[0], ...) });
31              
32             =head1 DESCRIPTION
33              
34             This module contains functions that examine and modify many aspects of
35             subroutines in Perl. It is intended to help in the implementation of
36             attribute handlers, and for other such special effects.
37              
38             =cut
39              
40             package Sub::Mutate;
41              
42 8     8   204826 { use 5.008; }
  8         27  
  8         641  
43 8     8   43 use warnings;
  8         16  
  8         236  
44 8     8   56 use strict;
  8         40  
  8         399  
45              
46             our $VERSION = "0.005";
47              
48 8     8   8288 use parent "Exporter";
  8         2473  
  8         41  
49             our @EXPORT_OK = qw(
50             sub_body_type
51             sub_closure_role
52             sub_is_lvalue
53             sub_is_constant
54             sub_is_method mutate_sub_is_method
55             sub_is_debuggable mutate_sub_is_debuggable
56             sub_prototype mutate_sub_prototype
57             when_sub_bodied
58             );
59              
60             require XSLoader;
61             XSLoader::load(__PACKAGE__, $VERSION);
62              
63             =head1 FUNCTIONS
64              
65             Each of these functions takes an argument I<SUB>, which must be a
66             reference to a subroutine. The function operates on the subroutine
67             referenced by I<SUB>.
68              
69             The C<mutate_> functions modify a subroutine in place. The subroutine's
70             identity is not changed, but the attributes of the existing subroutine
71             object are changed. All references to the existing subroutine will see
72             the new attributes. Beware of action at a distance.
73              
74             =over
75              
76             =item sub_body_type(SUB)
77              
78             Returns a keyword indicating the general nature of the implementation
79             of I<SUB>:
80              
81             =over
82              
83             =item B<PERL>
84              
85             The subroutine's body consists of a network of op nodes for the Perl
86             interpreter. Subroutines written in Perl are almost always of this form.
87              
88             =item B<UNDEF>
89              
90             The subroutine has no body, and so cannot be successfully called.
91              
92             =item B<XSUB>
93              
94             The subroutine's body consists of native machine code. Usually these
95             subroutines have been mainly written in C. Constant-valued subroutines
96             written in Perl can also acquire this type of body.
97              
98             =back
99              
100             =item sub_closure_role(SUB)
101              
102             Returns a keyword indicating the status of I<SUB> with respect to the
103             generation of closures in the Perl language:
104              
105             =over
106              
107             =item B<CLOSURE>
108              
109             The subroutine is a closure: it was generated from Perl code referencing
110             external lexical variables, and now references a particular set of those
111             variables to make up a complete subroutine.
112              
113             =item B<PROTOTYPE>
114              
115             The subroutine is a prototype for closures: it consists of Perl code
116             referencing external lexical variables, and has not been attached to a
117             particular set of those variables. This is not a complete subroutine
118             and cannot be successfully called. It is an oddity of Perl that this
119             type of object is represented as if it were a subroutine, and the
120             situations where one can get access to this kind of object are rare.
121             Prototype subroutines will mainly be encountered by attribute handlers.
122              
123             =item B<STANDALONE>
124              
125             The subroutine is independent of external lexical variables.
126              
127             =back
128              
129             =item sub_is_lvalue(SUB)
130              
131             Returns a truth value indicating whether I<SUB> is expected to return an
132             lvalue. An lvalue subroutine is usually created by using the C<:lvalue>
133             attribute, which affects how the subroutine body is compiled and also
134             sets the flag that this function extracts.
135              
136             =item sub_is_constant(SUB)
137              
138             Returns a truth value indicating whether I<SUB> returns a constant
139             value and can therefore be inlined. It is possible for a subroutine
140             to actually be constant-valued without the compiler detecting it and
141             setting this flag.
142              
143             =item sub_is_method(SUB)
144              
145             Returns a truth value indicating whether I<SUB> is marked as a method.
146             This marker can be applied by use of the C<:method> attribute, and
147             (as of Perl 5.10) affects almost nothing.
148              
149             =item mutate_sub_is_method(SUB, NEW_METHODNESS)
150              
151             Marks or unmarks I<SUB> as a method, depending on the truth value of
152             I<NEW_METHODNESS>.
153              
154             =item sub_is_debuggable(SUB)
155              
156             Returns a truth value indicating whether, when the Perl debugger
157             is activated, calls to I<SUB> can be intercepted by C<DB::sub> (see
158             L<perldebguts>). Normally this is true for all subroutines, but note
159             that whether a particular call is intercepted also depends on the nature
160             of the calling site.
161              
162             =item mutate_sub_is_debuggable(SUB, NEW_DEBUGGABILITY)
163              
164             Changes whether the Perl debugger will intercept calls to I<SUB>,
165             depending on the truth value of I<NEW_DEBUGGABILITY>.
166              
167             =item sub_prototype(SUB)
168              
169             Returns the prototype of I<SUB>, which is a string, or C<undef> if the
170             subroutine has no prototype. (No prototype is different from the empty
171             string prototype.) Prototypes affect the compilation of calls to the
172             subroutine, where the identity of the called subroutine can be resolved
173             at compile time. (This is unrelated to the closure prototypes described
174             for L</sub_closure_role>.)
175              
176             =item mutate_sub_prototype(SUB, NEW_PROTOTYPE)
177              
178             Sets or deletes the prototype of I<SUB>, to match I<NEW_PROTOTYPE>,
179             which must be either a string or C<undef>.
180              
181             =item when_sub_bodied(SUB, ACTION)
182              
183             Queues a modification of I<SUB>, to occur when I<SUB> has acquired a body.
184             This is required due to an oddity of how Perl constructs Perl-language
185             subroutines. A subroutine object is initially created with no body,
186             and then the body is later attached. Prior to Perl 5.15.4, attribute
187             handlers are executed before the body is attached, so see it in that
188             intermediate state. (From Perl 5.15.4 onwards, attribute handlers are
189             executed after the body is attached.) It is otherwise unusual to see
190             the subroutine in that intermediate state. If the implementation of an
191             attribute can only be completed after the body is attached, this function
192             is the way to schedule the implementation.
193              
194             If this function is called when I<SUB> is in the intermediate state, with
195             body not yet attached, then I<ACTION> is added to a queue. Shortly after
196             a body is attached to I<SUB>, the queued actions are performed. I<ACTION>
197             must be a reference to a function, which is called with one argument, a
198             reference to the subroutine to act on. The subroutine passed to I<ACTION>
199             is not necessarily the same object as the original I<SUB>: some subroutine
200             construction sequences cause the partially-built subroutine to move from
201             one object to another part way through, and the queue of pending actions
202             moves with it.
203              
204             If this function is called when I<SUB> already has a body, the action
205             will be performed immediately, or nearly so. Actions are always
206             performed sequentially, in the order in which they were queued, so if
207             an action is requested while another action is already executing then
208             the newly-requested action will have to wait until the executing one
209             has finished.
210              
211             If a subroutine with pending actions is replaced, in the same subroutine
212             object, by a new subroutine, then the queue of pending actions is
213             discarded. This occurs in the case of a so-called "forward declaration",
214             such as "C<sub foo ($);>". The declaration creates a subroutine with
215             no body, to influence compilation of calls to the subroutine, and it
216             is intended that the empty subroutine will later be replaced by a full
217             subroutine which has a body.
218              
219             =back
220              
221             =head1 BUGS
222              
223             The code behind C<when_sub_bodied> is an ugly experimental hack, which
224             may turn out to be fragile. Details of its behaviour may change in
225             future versions of this module, if better ways of achieving the desired
226             effect are found.
227              
228             Before Perl 5.10, C<when_sub_bodied> has a particular problem with
229             redefining subroutines. A subroutine redefinition, including if the
230             previous definition had no body (a pre-declaration), is the situation
231             that causes a partially-built subroutine to move from one subroutine
232             object to another. On pre-5.10 Perls, it is impossible to locate the
233             destination object at the critical point in this process, and as a result
234             any pending actions are lost.
235              
236             =head1 SEE ALSO
237              
238             L<Attribute::Lexical>,
239             L<B::CallChecker>
240              
241             =head1 AUTHOR
242              
243             Andrew Main (Zefram) <zefram@fysh.org>
244              
245             =head1 COPYRIGHT
246              
247             Copyright (C) 2009, 2010, 2011, 2013
248             Andrew Main (Zefram) <zefram@fysh.org>
249              
250             =head1 LICENSE
251              
252             This module is free software; you can redistribute it and/or modify it
253             under the same terms as Perl itself.
254              
255             =cut
256              
257             1;