PPI/Statement/Include.pm

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