line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
2
|
|
|
2
|
|
1922
|
use 5.008; |
|
2
|
|
|
|
|
9
|
|
|
2
|
|
|
|
|
90
|
|
2
|
2
|
|
|
2
|
|
14
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
80
|
|
3
|
2
|
|
|
2
|
|
22
|
use warnings; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
123
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
package Sub::StopCalls; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
8
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
11
|
use XSLoader; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
178
|
|
10
|
|
|
|
|
|
|
XSLoader::load( __PACKAGE__, $VERSION ); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 NAME |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
Sub::StopCalls - stop sub calls (make it a constant) |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 SYNOPSIS |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
my $i = 0; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub boo { |
21
|
|
|
|
|
|
|
return foo(); |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
sub foo { |
24
|
|
|
|
|
|
|
$i++; |
25
|
|
|
|
|
|
|
return Sub::StopCalls::stop(); |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
print "$i\n"; # 0 |
29
|
|
|
|
|
|
|
boo(); |
30
|
|
|
|
|
|
|
print "$i\n"; # 1 |
31
|
|
|
|
|
|
|
boo(); |
32
|
|
|
|
|
|
|
print "$i\n"; # 1 |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=head1 DESCRIPTION |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
Basicly you can do the following in a function to mean "Hey, You! |
37
|
|
|
|
|
|
|
You, who called me, stop calling me, I will always return |
38
|
|
|
|
|
|
|
the same result": |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
return Sub::StopCalls::stop( @result ); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
Still no idea how to use? Ok, here some use cases: |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=head1 USE CASES |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=head2 conditional constants |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
Classic C thing: |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub debug { |
51
|
|
|
|
|
|
|
return Sub::StopCalls::stop() unless $ENV{'MY_APP_DEBUG'}; |
52
|
|
|
|
|
|
|
... |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
Or logger: |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
package My::Logger; |
58
|
|
|
|
|
|
|
sub warn { |
59
|
|
|
|
|
|
|
return Sub::StopCalls::stop() if $self->{max_level} < LEVEL_WARN; |
60
|
|
|
|
|
|
|
... |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=head2 accessors to singletons |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
package MyApp; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
my $system; |
68
|
|
|
|
|
|
|
sub system { |
69
|
|
|
|
|
|
|
$system ||= do { |
70
|
|
|
|
|
|
|
... init system object ... |
71
|
|
|
|
|
|
|
}; |
72
|
|
|
|
|
|
|
return Sub::StopCalls::stop( $system ); |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=head2 hooks, triggers and callbacks |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub trigger { |
78
|
|
|
|
|
|
|
my $self = shift; |
79
|
|
|
|
|
|
|
my @triggers = $self->find_triggers(caller); |
80
|
|
|
|
|
|
|
return Sub::StopCalls::stop() unless @triggers; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
... |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head1 FUNCTIONS |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head2 stop |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
Does the job. Replaces call on upper level with whatever |
90
|
|
|
|
|
|
|
is passed into the function. Expected usage: |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
return Sub::StopCalls::stop(...) if ...; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
Some details |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head3 context |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
Result depends on context of the call that is replaced. |
99
|
|
|
|
|
|
|
Nothing special about void or array context, however, |
100
|
|
|
|
|
|
|
in scalar context if more than one argument passed into |
101
|
|
|
|
|
|
|
the function then number of elements returned: |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# replaces with undef |
104
|
|
|
|
|
|
|
sub foo { return Sub::StopCalls::stop(); } |
105
|
|
|
|
|
|
|
# replaces with 'const' |
106
|
|
|
|
|
|
|
sub foo { return Sub::StopCalls::stop( 'const' ); } |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
# replaces with number of element in @a, |
109
|
|
|
|
|
|
|
# but only if @a > 1, otherwise first element or undef |
110
|
|
|
|
|
|
|
return Sub::StopCalls::stop( @a ); |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head3 arguments |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
Arguments of the replaced call also "stopped", for example: |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
for (1..10) { |
117
|
|
|
|
|
|
|
function_that_stops_calls( other(...) ); |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
C called only once. Second iteration just jumps |
121
|
|
|
|
|
|
|
over. |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
It's good in theory, but in some situations it can result in |
124
|
|
|
|
|
|
|
bugs. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head3 threads |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
This module is not thread-safe at the moment. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=cut |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head1 AUTHOR |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
Ruslan Zakirov Eruz@bestpractical.comE |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head1 LICENSE |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Under the same terms as perl itself. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=cut |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
1; |