PPI/Statement/Include.pm

filter: Statementstmt 100% 48 / 48 Branchbran 72.2% 26 / 36 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 12 / 12 Podpod 100% 7 / 7 totaltotal 89.3% 101 / 113 CC 21 SCAR 13.7
CC
21
Cov
88%
arguments
7.0
module_version
5.0
type
3.2
CRAP 3.9
« PPI/Token/Structure.pm ↑ Summary PPI/Statement/Sub.pm »
1 package PPI::Statement::Include;
2  
3 =pod
4  
5 =head1 NAME
6  
7 PPI::Statement::Include - Statements that include other code
8  
9 =head1 SYNOPSIS
10  
11 # The following are all includes
12 use 5.006;
13 use strict;
14 use My::Module;
15 use constant FOO => 'Foo';
16 require Foo::Bar;
17 require "Foo/Bar.pm";
18 require $foo if 1;
19 no strict 'refs';
20  
21 =head1 INHERITANCE
22  
23 PPI::Statement::Include
24 isa PPI::Statement
25 isa PPI::Node
26 isa PPI::Element
27  
28 =head1 DESCRIPTION
29  
30 Despite its name, the C<PPI::Statement::Include> class covers a number
31 of different types of statement that cover all statements starting with
32 C<use>, C<no> and C<require>.
33  
34 But basically, they cover three situations.
35  
36 Firstly, a dependency on a particular version of perl (for which the
37 C<version> method returns true), a pragma (for which the C<pragma> method
38 returns true), or the loading (and unloading via no) of modules.
39  
40 =head1 METHODS
41  
42 C<PPI::Statement::Include> has a number of methods in addition to the standard
43 L<PPI::Statement>, L<PPI::Node> and L<PPI::Element> methods.
44  
45 =cut
46  
47 62 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 62 ▶use PPI::Statement ();
Subroutine BEGIN: called CC=1 SCAR=0.0
49 62 ▶use PPI::Statement::Include::Perl6 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 62 ▶use vars qw{$VERSION @ISA};
Subroutine BEGIN: called CC=1 SCAR=0.0
52 BEGIN {
53 62 ▶ $VERSION = '1.236';
Subroutine BEGIN: called CC=1 SCAR=0.0
54 62 @ISA = 'PPI::Statement';
55 }
56  
57 =pod
58  
59 =head2 type
60  
61 The C<type> method returns the general type of statement (C<'use'>, C<'no'>
62 or C<'require'>).
63  
64 Returns the type as a string, or C<undef> if the type cannot be detected.
65  
66 =cut
67  
68 sub type {
69 7 ▶ my $self = shift;
Subroutine type: called CC=3 SCAR=11.7
Pod documented
70 7 ▶ my $keyword = $self->schild(0) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $keyword = $self->schild(0) or return undef 0 7 7
71 7 ▶ $keyword->isa('PPI::Token::Word') and $keyword->content;
Branch: 1 branch
BranchTrueFalseTotal
$keyword->isa("PPI::Token::Word") and $keyword->content 7 0 7
72 }
73  
74 =pod
75  
76 =head2 module
77  
78 The C<module> method returns the module name specified in any include
79 statement. This C<includes> pragma names, because pragma are implemented
80 as modules. (And lets face it, the definition of a pragma can be fuzzy
81 at the best of times in any case)
82  
83 This covers all of these...
84  
85 use strict;
86 use My::Module;
87 no strict;
88 require My::Module;
89  
90 ...but does not cover any of these...
91  
92 use 5.006;
93 require 5.005;
94 require "explicit/file/name.pl";
95  
96 Returns the module name as a string, or C<undef> if the include does
97 not specify a module name.
98  
99 =cut
100  
101 sub module {
102 2 ▶ my $self = shift;
Subroutine module: called CC=3 SCAR=11.7
Pod documented
103 2 ▶ my $module = $self->schild(1) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $module = $self->schild(1) or return undef 0 2 2
104 2 ▶ $module->isa('PPI::Token::Word') and $module->content;
Branch: 1 branch
BranchTrueFalseTotal
$module->isa("PPI::Token::Word") and $module->content 2 0 2
105 }
106  
107 =pod
108  
109 =head2 module_version
110  
111 The C<module_version> method returns the minimum version of the module
112 required by the statement, if there is one.
113  
114 =cut
115  
116 sub module_version {
117 9 ▶ my $self = shift;
Subroutine module_version: called CC=5 SCAR=16.1
Pod documented
118 9 my $argument = $self->schild(3);
119 9 ▶ if ( $argument and $argument->isa('PPI::Token::Operator') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($argument and $argument->isa("PPI::Token::Operator")) 1 8 9
Condition100%3/3
$argument and $argument->isa("PPI::Token::Operator")
!ll&&!rl&&r
261
MC/DC100%2/2
$argument and $argument->isa("PPI::Token::Operator")
$argument $argument->isa("PPI::Token::Operator")
Truth table100%3/3
A and B
A= $argumentB= $argument->isa("PPI::Token::Operator")
ABresult
0X0
100
111
120 1 return undef;
121 }
122  
123 8 ▶ my $version = $self->schild(2) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $version = $self->schild(2) or return undef 0 8 8
124 8 ▶ return undef unless $version->isa('PPI::Token::Number');
Branch: 1 branch
BranchTrueFalseTotal
unless $version->isa("PPI::Token::Number") 3 5 8
125  
126 5 return $version;
127 }
128  
129 =pod
130  
131 =head2 pragma
132  
133 The C<pragma> method checks for an include statement's use as a
134 pragma, and returns it if so.
135  
136 Or at least, it claims to. In practice it's a lot harder to say exactly
137 what is or isn't a pragma, because the definition is fuzzy.
138  
139 The C<intent> of a pragma is to modify the way in which the parser works.
140 This is done though the use of modules that do various types of internals
141 magic.
142  
143 For now, PPI assumes that any "module name" that is only a set of
144 lowercase letters (and perhaps numbers, like C<use utf8;>). This
145 behaviour is expected to change, most likely to something that knows
146 the specific names of the various "pragmas".
147  
148 Returns the name of the pragma, or false ('') if the include is not a
149 pragma.
150  
151 =cut
152  
153 sub pragma {
154 2 ▶ my $self = shift;
Subroutine pragma: called CC=3 SCAR=11.7
Pod documented
155 2 ▶ my $module = $self->module or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $module = $self->module or return "" 0 2 2
156 2 ▶ $module =~ /^[a-z][a-z\d]*$/ ? $module : '';
Branch: 1 branch
BranchTrueFalseTotal
$module =~ /^[a-z][a-z\d]*$/ ? : 2 0 2
157 }
158  
159 =pod
160  
161 =head2 version
162  
163 The C<version> method checks for an include statement that introduces a
164 dependency on the version of C<perl> the code is compatible with.
165  
166 This covers two specific statements.
167  
168 use 5.006;
169 require 5.006;
170  
171 Currently the version is returned as a string, although in future the version
172 may be returned as a L<version> object. If you want a numeric representation,
173 use C<version_literal()>. Returns false if the statement is not a version
174 dependency.
175  
176 =cut
177  
178 sub version {
179 11 ▶ my $self = shift;
Subroutine version: called CC=3 SCAR=11.1
Pod documented
180 11 ▶ my $version = $self->schild(1) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $version = $self->schild(1) or return undef 0 11 11
181 11 ▶ $version->isa('PPI::Token::Number') ? $version->content : '';
Branch: 1 branch
BranchTrueFalseTotal
$version->isa('PPI::Token::Number') ? : 10 1 11
182 }
183  
184 =pod
185  
186 =head2 version_literal
187  
188 The C<version_literal> method has the same behavior as C<version()>, but the
189 version is returned as a numeric literal. Returns false if the statement is
190 not a version dependency.
191  
192 =cut
193  
194 sub version_literal {
195 11 ▶ my $self = shift;
Subroutine version_literal: called CC=3 SCAR=11.1
Pod documented
196 11 ▶ my $version = $self->schild(1) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $version = $self->schild(1) or return undef 0 11 11
197 11 ▶ $version->isa('PPI::Token::Number') ? $version->literal : '';
Branch: 1 branch
BranchTrueFalseTotal
$version->isa('PPI::Token::Number') ? : 10 1 11
198 }
199  
200 =pod
201  
202 =head2 arguments
203  
204 The C<arguments> method gives you the rest of the statement after the
205 module/pragma and module version, i.e. the stuff that will be used to
206 construct what gets passed to the module's C<import()> subroutine. This does
207 include the comma, etc. operators, but doesn't include non-significant direct
208 children or any final semicolon.
209  
210 =cut
211  
212 sub arguments {
213 7 ▶ my $self = shift;
Subroutine arguments: called CC=7 SCAR=19.5
Pod documented
214 7 my @args = $self->schildren;
215  
216 # Remove the "use", "no" or "require"
217 7 shift @args;
218  
219 # Remove the statement terminator
220 7 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($args[-1]->isa("PPI::Token::Structure") and $args[-1]->content eq ";") 6 1 7
Condition66%2/3
$args[-1]->isa("PPI::Token::Structure") and $args[-1]->content eq ";"
!ll&&!rl&&r
106
MC/DC50%1/2
$args[-1]->isa("PPI::Token::Structure") and $args[-1]->content eq ";"
$args[-1]->isa("PPI::Token::Structure") $args[-1]->content eq ";"
Truth table66%2/3
A and B
A= $args[-1]->isa("PPI::Token::Structure")B= $args[-1]->content eq ";"
ABresult
0X0
100
111
221 $args[-1]->isa('PPI::Token::Structure')
222 and
223 $args[-1]->content eq ';'
224 ) {
225 6 pop @args;
226 }
227  
228 # Remove the module or perl version.
229 7 shift @args;
230  
231 7 ▶ return unless @args;
Branch: 1 branch
BranchTrueFalseTotal
unless @args 2 5 7
232  
233 5 ▶ if ( $args[0]->isa('PPI::Token::Number') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args[0]->isa("PPI::Token::Number")) 2 3 5
234 2 ▶ my $after = $args[1] or return;
Branch: 1 branch
BranchTrueFalseTotal
my $after = $args[1] or return 1 1 2
235 1 ▶ $after->isa('PPI::Token::Operator') or shift @args;
Branch: 1 branch
BranchTrueFalseTotal
$after->isa("PPI::Token::Operator") or shift @args 1 0 1
236 }
237  
238 4 return @args;
239 }
240  
241 62 1;
242  
243 =pod
244  
245 =head1 TO DO
246  
247 - Write specific unit tests for this package
248  
249 =head1 SUPPORT
250  
251 See the L<support section|PPI/SUPPORT> in the main module.
252  
253 =head1 AUTHOR
254  
255 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
256  
257 =head1 COPYRIGHT
258  
259 Copyright 2001 - 2011 Adam Kennedy.
260  
261 This program is free software; you can redistribute
262 it and/or modify it under the same terms as Perl itself.
263  
264 The full text of the license can be found in the
265 LICENSE file included with this module.
266  
267 =cut
« PPI/Token/Structure.pm ↑ Summary PPI/Statement/Sub.pm »