PPI/Token/Magic.pm

filter: Statementstmt 95.6% 66 / 69 Branchbran 91.1% 31 / 34 Conditioncond 86.6% 13 / 15 MC/DCmcdc 77.7% 7 / 9 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 92.5% 125 / 135 CC 24 SCAR 29.0
CC
24
Cov
93%
__TOKENIZER__on_char
23.3
BEGIN
2.0
BEGIN
1.0
CRAP 18.1
« PPI/Normal/Standard.pm ↑ Summary PPI/Dumper.pm »
1 package PPI::Token::Magic;
2  
3 =pod
4  
5 =head1 NAME
6  
7 PPI::Token::Magic - Tokens representing magic variables
8  
9 =head1 INHERITANCE
10  
11 PPI::Token::Magic
12 isa PPI::Token::Symbol
13 isa PPI::Token
14 isa PPI::Element
15  
16 =head1 SYNOPSIS
17  
18 # When we say magic variables, we mean these...
19 $1 $2 $3 $4 $5 $6 $7 $8 $9
20 $_ $& $` $' $+ @+ %+ $* $. $/ $|
21 $\ $" $; $% $= $- @- %- $) $#
22 $~ $^ $: $? $! %! $@ $$ $< $>
23 $( $0 $[ $] @_ @* $} $, $#+ $#-
24 $^L $^A $^E $^C $^D $^F $^H
25 $^I $^M $^N $^O $^P $^R $^S
26 $^T $^V $^W $^X %^H
27  
28 =head1 DESCRIPTION
29  
30 C<PPI::Token::Magic> is a sub-class of L<PPI::Token::Symbol> which
31 identifies the token as "magic variable", one of the strange and
32 unusual variables that are connected to "things" behind the scenes.
33  
34 Some are extremely common, like C<$_>, and others you will quite
35 probably never encounter in your Perl career.
36  
37 =head1 METHODS
38  
39 The class provides no additional methods, beyond those provided by
40 L<PPI::Token::Symbol>, L<PPI::Token> and L<PPI::Element>.
41  
42 =cut
43  
44 62 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 62 ▶use PPI::Token::Symbol ();
Subroutine BEGIN: called CC=1 SCAR=0.0
46 62 ▶use PPI::Token::Unknown ();
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 62 ▶use vars qw{$VERSION @ISA %magic};
Subroutine BEGIN: called CC=1 SCAR=0.0
49 BEGIN {
50 62 ▶ $VERSION = '1.236';
Subroutine BEGIN: called CC=2 SCAR=6.9
51 62 @ISA = 'PPI::Token::Symbol';
52  
53 # Magic variables taken from perlvar.
54 # Several things added separately to avoid warnings.
55 62 foreach ( qw{
56 $1 $2 $3 $4 $5 $6 $7 $8 $9
57 $_ $& $` $' $+ @+ %+ $* $. $/ $|
58 $\\ $" $; $% $= $- @- %- $)
59 $~ $^ $: $? $! %! $@ $$ $< $>
60 $( $0 $[ $] @_ @*
61  
62 $^L $^A $^E $^C $^D $^F $^H
63 $^I $^M $^N $^O $^P $^R $^S
64 $^T $^V $^W $^X %^H
65  
66 $::|
67 }, '$}', '$,', '$#', '$#+', '$#-' ) {
68 4340 $magic{$_} = 1;
69 }
70 }
71  
72  
73 sub __TOKENIZER__on_char {
74 1301 ▶ my $t = $_[1];
Subroutine __TOKENIZER__on_char: called CC=23 SCAR=31.5
75  
76 # $c is the candidate new content
77 1301 my $c = $t->{token}->{content} . substr( $t->{line}, $t->{line_cursor}, 1 );
78  
79 # Do a quick first test so we don't have to do more than this one.
80 # All of the tests below match this one, so it should provide a
81 # small speed up. This regex should be updated to match the inside
82 # tests if they are changed.
83 1301 ▶ if ( $c =~ /^ \$ .* [ \w : \$ \{ ] $/x ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($c =~ /^ \$ .* [ \w : \$ \{ ] $/x) { } 891 410 1301
elsif ($c =~ /^%\^/) { } 17 393 410
84  
85 891 ▶ if ( $c =~ /^(\$(?:\_[\w:]|::))/ or $c =~ /^\$\'[\w]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /^(\$(?:\_[\w:]|::))/ or $c =~ /^\$\'[\w]/) 88 803 891
Condition100%3/3
$c =~ /^(\$(?:\_[\w:]|::))/ or $c =~ /^\$\'[\w]/
l!l&&r!l&&!r
5038803
MC/DC100%2/2
$c =~ /^(\$(?:\_[\w:]|::))/ or $c =~ /^\$\'[\w]/
$c =~ /^(\$(?:\_[\w:]|::))/ $c =~ /^\$\'[\w]/
Truth table100%3/3
A or B
A= $c =~ /^(\$(?:\_[\w:]|::))/B= $c =~ /^\$\'[\w]/
ABresult
1X1
011
000
86 # If and only if we have $'\d, it is not a
87 # symbol. (this was apparently a conscious choice)
88 # Note that $::0 on the other hand is legal
89 88 ▶ if ( $c =~ /^\$\'\d$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /^\$\'\d$/) 2 86 88
90 # In this case, we have a magic plus a digit.
91 # Save the CURRENT token, and rerun the on_char
92 2 return $t->_finalize_token->__TOKENIZER__on_char( $t );
93 }
94  
95 # A symbol in the style $_foo or $::foo or $'foo.
96 # Overwrite the current token
97 86 $t->{class} = $t->{token}->set_class('Symbol');
98 86 return PPI::Token::Symbol->__TOKENIZER__on_char( $t );
99 }
100  
101 803 ▶ if ( $c =~ /^\$\$\w/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /^\$\$\w/) 76 727 803
102 # This is really a scalar dereference. ( $$foo )
103 # Add the current token as the cast...
104 76 $t->{token} = PPI::Token::Cast->new( '$' );
105 76 $t->_finalize_token;
106  
107 # ... and create a new token for the symbol
108 76 return $t->_new_token( 'Symbol', '$' );
109 }
110  
111 727 ▶ if ( $c eq '$${' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\$\${") 1 726 727
112 # This _might_ be a dereference of one of the
113 # control-character symbols.
114 1 pos $t->{line} = $t->{line_cursor} + 1;
115 1 ▶ if ( $t->{line} =~ m/$PPI::Token::Unknown::CURLY_SYMBOL/gc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{"line"} =~ /$PPI::Token::Unknown::CURLY_SYMBOL/cg) 1 0 1
116 # This is really a dereference. ( $${^_foo} )
117 # Add the current token as the cast...
118 1 $t->{token} = PPI::Token::Cast->new( '$' );
119 1 $t->_finalize_token;
120  
121 # ... and create a new token for the symbol
122 1 return $t->_new_token( 'Magic', '$' );
123 }
124 }
125  
126 726 ▶ if ( $c eq '$#$' or $c eq '$#{' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\$#\$" or $c eq "\$#{") 4 722 726
Condition66%2/3
$c eq "\$#\$" or $c eq "\$#{"
l!l&&r!l&&!r
04722
MC/DC50%1/2
$c eq "\$#\$" or $c eq "\$#{"
$c eq "\$#\$" $c eq "\$#{"
Truth table66%2/3
A or B
A= $c eq "\$#\$"B= $c eq "\$#{"
ABresult
1X1
011
000
127 # This is really an index dereferencing cast, although
128 # it has the same two chars as the magic variable $#.
129 4 $t->{class} = $t->{token}->set_class('Cast');
130 4 return $t->_finalize_token->__TOKENIZER__on_char( $t );
131 }
132  
133 722 ▶ if ( $c =~ /^(\$\#)\w/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /^(\$\#)\w/) 97 625 722
134 # This is really an array index thingy ( $#array )
135 97 $t->{token} = PPI::Token::ArrayIndex->new( "$1" );
136 97 return PPI::Token::ArrayIndex->__TOKENIZER__on_char( $t );
137 }
138  
139 625 ▶ if ( $c =~ /^\$\^\w+$/o ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /^\$\^\w+$/o) 360 265 625
140 # It's an escaped char magic... maybe ( like $^M )
141 360 my $next = substr( $t->{line}, $t->{line_cursor}+1, 1 ); # Peek ahead
142 360 ▶ if ($magic{$c} && (!$next || $next !~ /\w/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($PPI::Token::Magic::magic{$c} and not $next && $next =~ /\w/) { } 33 327 360
Condition83%5/6
$PPI::Token::Magic::magic{$c} and not $next && $next =~ /\w/
!ll&&!rl&&r
3012633
$next && $next =~ /\w/
!ll&&!rl&&r
03326
MC/DC66%2/3
$PPI::Token::Magic::magic{$c} and not $next && $next =~ /\w/
$PPI::Token::Magic::magic{$c} $next $next =~ /\w/
Truth table75%3/4
A and not(B && C)
A= $PPI::Token::Magic::magic{$c}B= $nextC= $next =~ /\w/
ABCresult
0XX0
1110
10X1
1101
143 33 $t->{token}->{content} = $c;
144 33 $t->{line_cursor}++;
145 } else {
146 # Maybe it's a long magic variable like $^WIDE_SYSTEM_CALLS
147 327 return 1;
148 }
149 }
150  
151 298 ▶ if ( $c =~ /^\$\#\{/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /^\$\#\{/) 0 298 298
152 # The $# is actually a cast, and { is its block
153 # Add the current token as the cast...
154 0 $t->{token} = PPI::Token::Cast->new( '$#' );
155 0 $t->_finalize_token;
156  
157 # ... and create a new token for the block
158 0 return $t->_new_token( 'Structure', '{' );
159 }
160 } elsif ($c =~ /^%\^/) {
161 17 ▶ return 1 if $c eq '%^';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "%^" 0 17 17
162 # It's an escaped char magic... maybe ( like %^H )
163 17 ▶ if ($magic{$c}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($PPI::Token::Magic::magic{$c}) { } 8 9 17
164 8 $t->{token}->{content} = $c;
165 8 $t->{line_cursor}++;
166 } else {
167 # Back off, treat '%' as an operator
168 9 chop $t->{token}->{content};
169 9 bless $t->{token}, $t->{class} = 'PPI::Token::Operator';
170 9 $t->{line_cursor}--;
171 }
172 }
173  
174 708 ▶ if ( $magic{$c} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($PPI::Token::Magic::magic{$c}) { } 42 666 708
175 # $#+ and $#-
176 42 $t->{line_cursor} += length( $c ) - length( $t->{token}->{content} );
177 42 $t->{token}->{content} = $c;
178 } else {
179 666 pos $t->{line} = $t->{line_cursor};
180 666 ▶ if ( $t->{line} =~ m/($PPI::Token::Unknown::CURLY_SYMBOL)/gc ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($t->{'line'} =~ /($PPI::Token::Unknown::CURLY_SYMBOL)/cg) { } 7 659 666
elsif ($c =~ /^\$\d+$/ and $t->{'line'} =~ /\G(\d+)/cg) { } 4 1308 1312
181 # control character symbol (e.g. ${^MATCH})
182 7 $t->{token}->{content} .= $1;
183 7 $t->{line_cursor} += length $1;
184 ▶ } elsif ( $c =~ /^\$\d+$/ and $t->{line} =~ /\G(\d+)/gc ) {
Condition100%3/3
$c =~ /^\$\d+$/ and $t->{"line"} =~ /\G(\d+)/cg
!ll&&!rl&&r
65324
MC/DC100%2/2
$c =~ /^\$\d+$/ and $t->{"line"} =~ /\G(\d+)/cg
$c =~ /^\$\d+$/ $t->{"line"} =~ /\G(\d+)/cg
Truth table100%3/3
A and B
A= $c =~ /^\$\d+$/B= $t->{"line"} =~ /\G(\d+)/cg
ABresult
0X0
100
111
185 # Grab trailing digits of regex capture variables.
186 4 $t->{token}{content} .= $1;
187 4 $t->{line_cursor} += length $1;
188 }
189 }
190  
191 # End the current magic token, and recheck
192 708 $t->_finalize_token->__TOKENIZER__on_char( $t );
193 }
194  
195 # Our version of canonical is plain simple
196 17 ▶sub canonical { $_[0]->content }
Subroutine canonical: called CC=1 SCAR=0.0
Pod documented
197  
198 62 1;
199  
200 =pod
201  
202 =head1 SUPPORT
203  
204 See the L<support section|PPI/SUPPORT> in the main module.
205  
206 =head1 AUTHOR
207  
208 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
209  
210 =head1 COPYRIGHT
211  
212 Copyright 2001 - 2011 Adam Kennedy.
213  
214 This program is free software; you can redistribute
215 it and/or modify it under the same terms as Perl itself.
216  
217 The full text of the license can be found in the
218 LICENSE file included with this module.
219  
220 =cut
« PPI/Normal/Standard.pm ↑ Summary PPI/Dumper.pm »