| 1 | package PPI::Statement::Variable; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | PPI::Statement::Variable - Variable declaration statements | ||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | # All of the following are variable declarations | ||||||||||||||||||||||||||||||||||||||||||
| 12 | my $foo = 1; | ||||||||||||||||||||||||||||||||||||||||||
| 13 | my ($foo, $bar) = (1, 2); | ||||||||||||||||||||||||||||||||||||||||||
| 14 | our $foo = 1; | ||||||||||||||||||||||||||||||||||||||||||
| 15 | local $foo; | ||||||||||||||||||||||||||||||||||||||||||
| 16 | local $foo = 1; | ||||||||||||||||||||||||||||||||||||||||||
| 17 | LABEL: my $foo = 1; | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | =head1 INHERITANCE | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | PPI::Statement::Variable | ||||||||||||||||||||||||||||||||||||||||||
| 22 | isa PPI::Statement::Expression | ||||||||||||||||||||||||||||||||||||||||||
| 23 | isa PPI::Statement | ||||||||||||||||||||||||||||||||||||||||||
| 24 | isa PPI::Node | ||||||||||||||||||||||||||||||||||||||||||
| 25 | isa PPI::Element | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | The main intent of the C<PPI::Statement::Variable> class is to describe | ||||||||||||||||||||||||||||||||||||||||||
| 30 | simple statements that explicitly declare new local or global variables. | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | Note that this does not make it exclusively the only place where variables | ||||||||||||||||||||||||||||||||||||||||||
| 33 | are defined, and later on you should expect that the C<variables> method | ||||||||||||||||||||||||||||||||||||||||||
| 34 | will migrate deeper down the tree to either L<PPI::Statement> or | ||||||||||||||||||||||||||||||||||||||||||
| 35 | L<PPI::Node> to recognise this fact, but for now it stays here. | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 62 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 62 | ▶ | use Params::Util qw{_INSTANCE}; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 62 | ▶ | use PPI::Statement::Expression (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 62 | ▶ | use vars qw{$VERSION @ISA}; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | BEGIN { | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 62 | ▶ | $VERSION = '1.236'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 62 | @ISA = 'PPI::Statement::Expression'; | |||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | =head2 type | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | The C<type> method checks and returns the declaration type of the statement, | ||||||||||||||||||||||||||||||||||||||||||
| 56 | which will be one of 'my', 'local', 'our', or 'state'. | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | Returns a string of the type, or C<undef> if the type cannot be detected | ||||||||||||||||||||||||||||||||||||||||||
| 59 | (which is probably a bug). | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | sub type { | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
type: called CC=4 SCAR=15.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | # Get the first significant child | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 5 | my @schild = grep { $_->significant } $self->children; | |||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | # Ignore labels | ||||||||||||||||||||||||||||||||||||||||||
| 70 | 5 | ▶ | shift @schild if _INSTANCE($schild[0], 'PPI::Token::Label'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | # Get the type | ||||||||||||||||||||||||||||||||||||||||||
| 73 | 5 | ▶ | (_INSTANCE($schild[0], 'PPI::Token::Word') and $schild[0]->content =~ /^(my|local|our|state)$/) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
&_INSTANCE($schild[0], "PPI::Token::Word") and $schild[0]->content =~ /^(my|local|our|state)$/
MC/DC0%0/2
&_INSTANCE($schild[0], "PPI::Token::Word") and $schild[0]->content =~ /^(my|local|our|state)$/
&_INSTANCE($schild[0], "PPI::Token::Word")
$schild[0]->content =~ /^(my|local|our|state)$/
Truth table33%1/3
A and B
A= &_INSTANCE($schild[0], "PPI::Token::Word")B= $schild[0]->content =~ /^(my|local|our|state)$/
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | ? $schild[0]->content | ||||||||||||||||||||||||||||||||||||||||||
| 75 | : undef; | ||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | =head2 variables | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | As for several other PDOM Element types that can declare variables, the | ||||||||||||||||||||||||||||||||||||||||||
| 83 | C<variables> method returns a list of the canonical forms of the variables | ||||||||||||||||||||||||||||||||||||||||||
| 84 | defined by the statement. | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | Returns a list of the canonical string forms of variables, or the null list | ||||||||||||||||||||||||||||||||||||||||||
| 87 | if it is unable to find any variables. | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | sub variables { | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 8 | ▶ | map { $_->canonical } $_[0]->symbols; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
variables: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | =head2 symbols | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | Returns a list of the variables defined by the statement, as | ||||||||||||||||||||||||||||||||||||||||||
| 100 | L<PPI::Token::Symbol>s. | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | sub symbols { | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 8 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
symbols: called CC=9 SCAR=23.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | # Get the children we care about | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 8 | my @schild = grep { $_->significant } $self->children; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 8 | ▶ | shift @schild if _INSTANCE($schild[0], 'PPI::Token::Label'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | # If the second child is a symbol, return its name | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 8 | ▶ | if ( _INSTANCE($schild[1], 'PPI::Token::Symbol') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 5 | return $schild[1]; | |||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | # If it's a list, return as a list | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 3 | ▶ | if ( _INSTANCE($schild[1], 'PPI::Structure::List') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 3 | my $Expression = $schild[1]->schild(0); | |||||||||||||||||||||||||||||||||||||||||
| 119 | 3 | ▶ | $Expression and | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$Expression and $Expression->isa("PPI::Statement::Expression")
MC/DC0%0/2
$Expression and $Expression->isa("PPI::Statement::Expression")
$Expression
$Expression->isa("PPI::Statement::Expression")
Truth table33%1/3
A and B
A= $ExpressionB= $Expression->isa("PPI::Statement::Expression")
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | $Expression->isa('PPI::Statement::Expression') or return (); | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | # my and our are simpler than local | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 3 | ▶ | if ( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$self->type eq "my" or $self->type eq "our" or $self->type eq "state"
$self->type eq "my" or $self->type eq "our"
MC/DC33%1/3
$self->type eq "my" or $self->type eq "our" or $self->type eq "state"
$self->type eq "my"
$self->type eq "our"
$self->type eq "state"
Truth table50%2/4
A or B or C
A= $self->type eq "my"B= $self->type eq "our"C= $self->type eq "state"
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | $self->type eq 'my' | ||||||||||||||||||||||||||||||||||||||||||
| 125 | or | ||||||||||||||||||||||||||||||||||||||||||
| 126 | $self->type eq 'our' | ||||||||||||||||||||||||||||||||||||||||||
| 127 | or | ||||||||||||||||||||||||||||||||||||||||||
| 128 | $self->type eq 'state' | ||||||||||||||||||||||||||||||||||||||||||
| 129 | ) { | ||||||||||||||||||||||||||||||||||||||||||
| 130 | return grep { | ||||||||||||||||||||||||||||||||||||||||||
| 131 | 2 | $_->isa('PPI::Token::Symbol') | |||||||||||||||||||||||||||||||||||||||||
| 132 | } $Expression->schildren; | ||||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | # Local is much more icky (potentially). | ||||||||||||||||||||||||||||||||||||||||||
| 136 | # Not that we are actually going to deal with it now, | ||||||||||||||||||||||||||||||||||||||||||
| 137 | # but having this separate is likely going to be needed | ||||||||||||||||||||||||||||||||||||||||||
| 138 | # for future bug reports about local() things. | ||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | # This is a slightly better way to check. | ||||||||||||||||||||||||||||||||||||||||||
| 141 | return grep { | ||||||||||||||||||||||||||||||||||||||||||
| 142 | 4 | $self->_local_variable($_) | |||||||||||||||||||||||||||||||||||||||||
| 143 | } grep { | ||||||||||||||||||||||||||||||||||||||||||
| 144 | 1 | $_->isa('PPI::Token::Symbol') | |||||||||||||||||||||||||||||||||||||||||
| 145 | } $Expression->schildren; | ||||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | # erm... this is unexpected | ||||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | (); | |||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | sub _local_variable { | ||||||||||||||||||||||||||||||||||||||||||
| 153 | 4 | ▶ | my ($self, $el) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_local_variable: called CC=6 SCAR=18.2
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | # The last symbol should be a variable | ||||||||||||||||||||||||||||||||||||||||||
| 156 | 4 | ▶ | my $n = $el->snext_sibling or return 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 3 | my $p = $el->sprevious_sibling; | |||||||||||||||||||||||||||||||||||||||||
| 158 | 3 | ▶ | if ( ! $p or $p eq ',' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $p or $p eq ","
MC/DC50%1/2
not $p or $p eq ","
not $p
$p eq ","
Truth table66%2/3
not(A) or B
A= not $pB= $p eq ","
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | # In the middle of a list | ||||||||||||||||||||||||||||||||||||||||||
| 160 | 1 | ▶ | return 1 if $n eq ','; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | # The first half of an assignment | ||||||||||||||||||||||||||||||||||||||||||
| 163 | 1 | ▶ | return 1 if $n eq '='; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | # Lets say no for know... additional work | ||||||||||||||||||||||||||||||||||||||||||
| 167 | # should go here. | ||||||||||||||||||||||||||||||||||||||||||
| 168 | 2 | return ''; | |||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | 62 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | =head1 TO DO | ||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | - Write unit tests for this | ||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | See the L<support section|PPI/SUPPORT> in the main module. | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | Copyright 2001 - 2011 Adam Kennedy. | ||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | This program is free software; you can redistribute | ||||||||||||||||||||||||||||||||||||||||||
| 192 | it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | The full text of the license can be found in the | ||||||||||||||||||||||||||||||||||||||||||
| 195 | LICENSE file included with this module. | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | =cut | ||||||||||||||||||||||||||||||||||||||||||