line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package pragma; |
2
|
2
|
|
|
2
|
|
52009
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
58
|
|
3
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
56
|
|
4
|
2
|
|
|
2
|
|
10
|
use Carp 'carp'; |
|
2
|
|
|
|
|
13
|
|
|
2
|
|
|
|
|
290
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
7
|
|
|
|
|
|
|
our $DEBUG; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 NAME |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
pragma - A pragma for controlling other user pragmas |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 DESCRIPTION |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
The C pragma is a module which influences other user pragmata |
16
|
|
|
|
|
|
|
such as L. With Perl 5.10 you can create user pragmata and the |
17
|
|
|
|
|
|
|
C pragma can modify and peek at other pragmata. |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 SUBCLASSING |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
All methods may be subclassed. Importing pragma with the single |
22
|
|
|
|
|
|
|
parameter '-base' will do the proper stuff so your class is now a |
23
|
|
|
|
|
|
|
pragma. |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
package your_pragma; |
26
|
|
|
|
|
|
|
use pragma -base; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# Woot! |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
1; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
Subclassed pragmas are stored in the hints hash with their package |
33
|
|
|
|
|
|
|
name as a prefix. This prevents pragmas from unintentionally stomping |
34
|
|
|
|
|
|
|
on each other. |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# sets 'your::pragma::foo = 42 |
37
|
|
|
|
|
|
|
use your_prama foo => 42; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=head1 A BASIC EXAMPLE |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
Assume you're using the C pragma mentioned in |
42
|
|
|
|
|
|
|
L. For ease, that pragma is duplicated here. You'll see it |
43
|
|
|
|
|
|
|
sets the C value to 1 when on and 0 when off. |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
package myint; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
use strict; |
48
|
|
|
|
|
|
|
use warnings; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub import { |
51
|
|
|
|
|
|
|
$^H{myint} = 1; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub unimport { |
55
|
|
|
|
|
|
|
$^H{myint} = 0; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
1; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
Other code might casually wish to dip into C: |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
no pragma 'myint'; # delete $^H{myint} |
63
|
|
|
|
|
|
|
use pragma myint => 42; # $^H{myint} = 42 |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
print pragma->peek( 'myint' ); # prints '42' |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
The above could have been written without the C module as: |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
BEGIN { delete $^H{myint} } |
70
|
|
|
|
|
|
|
BEGIN { $^H{myint} = 42 } |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
print $^H{myint}; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=cut |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head1 CLASS METHODS |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=over |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=item C<< use pragma PRAGMA => VALUE >> |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=item C<< pragma->import( PRAGMA => VALUE ) >> |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=item C<< pragma->poke( PRAGMA => VALUE ) >> |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
Sets C's value to C. |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=cut |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# TODO: figure out how to get Module::Compile::TT to integrate nicely |
91
|
|
|
|
|
|
|
# so instead of a pragma.pm and pragma.pmc I have in the source distro |
92
|
|
|
|
|
|
|
# a src/lib/pragma.pm and a lib/pragma.pm. |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# use tt subs => [qw[import poke]]; |
95
|
|
|
|
|
|
|
# [% FOREACH sub IN subs %] |
96
|
|
|
|
|
|
|
sub import { |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# Handle "use pragma;" |
99
|
7
|
100
|
|
7
|
|
3552
|
return if 1 == @_; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
# [% IF sub == 'import' %] |
102
|
|
|
|
|
|
|
# Handle "use pragma -base;" |
103
|
6
|
100
|
100
|
|
|
27
|
if ( 2 == @_ and $_[1] eq '-base' ) { |
104
|
2
|
|
|
2
|
|
11
|
no strict 'refs'; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
981
|
|
105
|
1
|
|
|
|
|
3
|
my $tgt = caller; |
106
|
1
|
50
|
|
|
|
9
|
carp "$tgt ISA $_[0]\n" if $DEBUG; |
107
|
1
|
|
|
|
|
2
|
@{ caller() . '::ISA' } = $_[0]; |
|
1
|
|
|
|
|
15
|
|
108
|
1
|
|
|
|
|
19
|
return; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
# [% END %] |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
# TODO: support "use pragma 'foo'" to mean "use pragma 'foo' => |
114
|
|
|
|
|
|
|
# '1'" |
115
|
|
|
|
|
|
|
|
116
|
5
|
|
|
|
|
8
|
my $class = shift @_; |
117
|
5
|
100
|
|
|
|
13
|
$class = $class eq __PACKAGE__ ? '' : "$class\::"; |
118
|
5
|
|
|
|
|
12
|
while (@_) { |
119
|
5
|
|
|
|
|
10
|
my ( $pragma, $value ) = splice @_, 0, 2; |
120
|
5
|
|
|
|
|
9
|
my $hh_pragma = "$class$pragma"; |
121
|
|
|
|
|
|
|
|
122
|
5
|
|
100
|
|
|
13
|
$value //= ''; |
123
|
5
|
50
|
|
|
|
10
|
carp "$hh_pragma = $value\n" if $DEBUG; |
124
|
5
|
|
|
|
|
27
|
$^H{$hh_pragma} = $value; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
5
|
|
|
|
|
189
|
return; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
# [% END ] |
131
|
|
|
|
|
|
|
# no tt; |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
sub poke { |
134
|
|
|
|
|
|
|
|
135
|
1
|
|
|
1
|
1
|
4
|
my $class = shift @_; |
136
|
1
|
50
|
|
|
|
4
|
$class = $class eq __PACKAGE__ ? '' : "$class\::"; |
137
|
1
|
|
|
|
|
3
|
while (@_) { |
138
|
1
|
|
|
|
|
3
|
my ( $pragma, $value ) = splice @_, 0, 2; |
139
|
1
|
|
|
|
|
3
|
my $hh_pragma = "$class$pragma"; |
140
|
|
|
|
|
|
|
|
141
|
1
|
|
50
|
|
|
3
|
$value //= ''; |
142
|
1
|
50
|
|
|
|
3
|
carp "$hh_pragma = $value\n" if $DEBUG; |
143
|
1
|
|
|
|
|
3
|
$^H{$hh_pragma} = $value; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
1
|
|
|
|
|
43
|
return; |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=item C<< no pragma PRAGMA >> |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=item C<< pragma->unimport( PRAGMA ) >> |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
Unsets C. |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=cut |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
sub unimport { |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
# Handle "no pragma"; |
160
|
2
|
50
|
|
2
|
|
15
|
return if 1 == @_; |
161
|
|
|
|
|
|
|
|
162
|
2
|
|
|
|
|
3
|
my ( $class, $pragma ) = @_; |
163
|
2
|
100
|
|
|
|
7
|
$class = $class eq __PACKAGE__ ? '' : "$class\::"; |
164
|
2
|
|
|
|
|
4
|
my $hh_pragma = "$class$pragma"; |
165
|
|
|
|
|
|
|
|
166
|
2
|
50
|
|
|
|
22
|
delete $^H{$hh_pragma} if exists $^H{$hh_pragma}; |
167
|
2
|
|
|
|
|
3563
|
return; |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=item C<< pragma->peek( PRAGMA ) >> |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
Returns the current value of C. |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=cut |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
sub peek { |
177
|
10
|
|
|
10
|
1
|
2059
|
my ( $class, $pragma ) = @_; |
178
|
10
|
100
|
|
|
|
28
|
$class = $class eq __PACKAGE__ ? '' : "$class\::"; |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
# use Data::Dumper 'Dumper'; |
181
|
|
|
|
|
|
|
# my $cx = 0; |
182
|
|
|
|
|
|
|
# while ( caller $cx ) { |
183
|
|
|
|
|
|
|
# print Dumper( [ $cx, ( caller $cx )[10] ] ); |
184
|
|
|
|
|
|
|
# ++$cx; |
185
|
|
|
|
|
|
|
# } |
186
|
|
|
|
|
|
|
|
187
|
10
|
|
|
|
|
68
|
my $hints_hash = ( caller 0 )[10]; |
188
|
10
|
100
|
|
|
|
47
|
return unless $hints_hash; |
189
|
8
|
100
|
|
|
|
35
|
return unless exists $hints_hash->{"$class$pragma"}; |
190
|
6
|
|
|
|
|
48
|
return $hints_hash->{"$class$pragma"}; |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=back |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=cut |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
q[And I don't think an entire stallion of horses, or a tank, could stop you two from getting married.]; |