| 1 | package pragma; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Carp 'carp'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 7 | 2 | 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> pragma is a module which influences other user pragmata | ||||||||||||||||||||||||||||
| 16 | such as L<lint>. With Perl 5.10 you can create user pragmata and the | ||||||||||||||||||||||||||||
| 17 | C<pragma> 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<myint> pragma mentioned in | ||||||||||||||||||||||||||||
| 42 | L<perlpragma>. For ease, that pragma is duplicated here. You'll see it | ||||||||||||||||||||||||||||
| 43 | sets the C<myint> 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<myint>: | ||||||||||||||||||||||||||||
| 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<pragma> 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<PRAGMA>'s value to C<VALUE>. | ||||||||||||||||||||||||||||
| 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 | 7 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # Handle "use pragma;" | ||||||||||||||||||||||||||||
| 99 | 7 | ▶ | return if 1 == @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # [% IF sub == 'import' %] | ||||||||||||||||||||||||||||
| 102 | # Handle "use pragma -base;" | ||||||||||||||||||||||||||||
| 103 | 6 | ▶ | if ( 2 == @_ and $_[1] eq '-base' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
2 == @_ and $_[1] eq "-base"
MC/DC100%2/2
2 == @_ and $_[1] eq "-base"
2 == @_
$_[1] eq "-base"
Truth table100%3/3
A and B
A= 2 == @_B= $_[1] eq "-base"
| |||||||||||||||||||||||||||||
| 104 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 105 | 1 | my $tgt = caller; | |||||||||||||||||||||||||||
| 106 | 1 | ▶ | carp "$tgt ISA $_[0]\n" if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 1 | @{ caller() . '::ISA' } = $_[0]; | |||||||||||||||||||||||||||
| 108 | 1 | return; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # [% END %] | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # TODO: support "use pragma 'foo'" to mean "use pragma 'foo' => | ||||||||||||||||||||||||||||
| 114 | # '1'" | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 5 | my $class = shift @_; | |||||||||||||||||||||||||||
| 117 | 5 | ▶ | $class = $class eq __PACKAGE__ ? '' : "$class\::"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 5 | ▶ | while (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 5 | my ( $pragma, $value ) = splice @_, 0, 2; | |||||||||||||||||||||||||||
| 120 | 5 | my $hh_pragma = "$class$pragma"; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 5 | ▶ | $value //= ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$value //= ""
MC/DC100%1/1
$value //= ""
$value
Truth table100%2/2
A
A= $value
| |||||||||||||||||||||||||||||
| 123 | 5 | ▶ | carp "$hh_pragma = $value\n" if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 5 | $^H{$hh_pragma} = $value; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 5 | return; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # [% END ] | ||||||||||||||||||||||||||||
| 131 | # no tt; | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 1 | ▶ | sub poke { | ||||||||||||||||||||||||||
|
Subroutine
poke: called CC=5 SCAR=16.4
Pod
documented
| |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 1 | my $class = shift @_; | |||||||||||||||||||||||||||
| 136 | 1 | ▶ | $class = $class eq __PACKAGE__ ? '' : "$class\::"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 1 | ▶ | while (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 1 | my ( $pragma, $value ) = splice @_, 0, 2; | |||||||||||||||||||||||||||
| 139 | 1 | my $hh_pragma = "$class$pragma"; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 1 | ▶ | $value //= ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$value //= ""
MC/DC0%0/1
$value //= ""
$value
Truth table50%1/2
A
A= $value
| |||||||||||||||||||||||||||||
| 142 | 1 | ▶ | carp "$hh_pragma = $value\n" if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 1 | $^H{$hh_pragma} = $value; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 1 | return; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =item C<< no pragma PRAGMA >> | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =item C<< pragma->unimport( PRAGMA ) >> | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | Unsets C<PRAGMA>. | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =cut | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 2 | ▶ | sub unimport { | ||||||||||||||||||||||||||
|
Subroutine
unimport: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | # Handle "no pragma"; | ||||||||||||||||||||||||||||
| 160 | 2 | ▶ | return if 1 == @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 2 | my ( $class, $pragma ) = @_; | |||||||||||||||||||||||||||
| 163 | 2 | ▶ | $class = $class eq __PACKAGE__ ? '' : "$class\::"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 2 | my $hh_pragma = "$class$pragma"; | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 2 | ▶ | delete $^H{$hh_pragma} if exists $^H{$hh_pragma}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 2 | return; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =item C<< pragma->peek( PRAGMA ) >> | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | Returns the current value of C<PRAGMA>. | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =cut | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 10 | ▶ | sub peek { | ||||||||||||||||||||||||||
|
Subroutine
peek: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 177 | 10 | my ( $class, $pragma ) = @_; | |||||||||||||||||||||||||||
| 178 | 10 | ▶ | $class = $class eq __PACKAGE__ ? '' : "$class\::"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | my $hints_hash = ( caller 0 )[10]; | |||||||||||||||||||||||||||
| 188 | 10 | ▶ | return unless $hints_hash; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | 8 | ▶ | return unless exists $hints_hash->{"$class$pragma"}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | 6 | return $hints_hash->{"$class$pragma"}; | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =back | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =cut | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 2 | q[And I don't think an entire stallion of horses, or a tank, could stop you two from getting married.]; | |||||||||||||||||||||||||||