File Coverage

blib/lib/PPI/Statement/Include.pm
Criterion Covered Total %
statement 42 42 100.0
branch 26 36 72.2
condition 5 6 83.3
subroutine 10 10 100.0
pod 7 7 100.0
total 90 101 89.1


line stmt bran cond sub pod time code
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 class covers a number
31             of different types of statement that cover all statements starting with
32             C, C and C.
33              
34             But basically, they cover three situations.
35              
36             Firstly, a dependency on a particular version of perl (for which the
37             C method returns true), a pragma (for which the C method
38             returns true), or the loading (and unloading via no) of modules.
39              
40             =head1 METHODS
41              
42             C has a number of methods in addition to the standard
43             L, L and L methods.
44              
45             =cut
46              
47 64     64   468 use strict;
  64         133  
  64         1843  
48 64     64   333 use PPI::Statement ();
  64         144  
  64         860  
49 64     64   28097 use PPI::Statement::Include::Perl6 ();
  64         184  
  64         34243  
50              
51             our $VERSION = '1.277';
52              
53             our @ISA = "PPI::Statement";
54              
55             =pod
56              
57             =head2 type
58              
59             The C 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 if the type cannot be detected.
63              
64             =cut
65              
66             sub type {
67 7     7 1 4770 my $self = shift;
68 7 50       27 my $keyword = $self->schild(0) or return undef;
69 7 50       40 $keyword->isa('PPI::Token::Word') and $keyword->content;
70             }
71              
72             =pod
73              
74             =head2 module
75              
76             The C method returns the module name specified in any include
77             statement. This C 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 if the include does
95             not specify a module name.
96              
97             =cut
98              
99             sub module {
100 2     2 1 4 my $self = shift;
101 2 50       12 my $module = $self->schild(1) or return undef;
102 2 50       26 $module->isa('PPI::Token::Word') and $module->content;
103             }
104              
105             =pod
106              
107             =head2 module_version
108              
109             The C 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     9 1 720 my $self = shift;
116 9         29 my $argument = $self->schild(3);
117 9 100 100     62 if ( $argument and $argument->isa('PPI::Token::Operator') ) {
118 1         7 return undef;
119             }
120              
121 8 50       18 my $version = $self->schild(2) or return undef;
122 8 100       62 return undef unless $version->isa('PPI::Token::Number');
123              
124 5         25 return $version;
125             }
126              
127             =pod
128              
129             =head2 pragma
130              
131             The C 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 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). 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     2 1 380 my $self = shift;
153 2 50       10 my $module = $self->module or return '';
154 2 50       22 $module =~ /^[a-z][a-z\d]*$/ ? $module : '';
155             }
156              
157             =pod
158              
159             =head2 version
160              
161             The C method checks for an include statement that introduces a
162             dependency on the version of C 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 object. If you want a numeric representation,
171             use C. Returns false if the statement is not a version
172             dependency.
173              
174             =cut
175              
176             sub version {
177 11     11 1 831 my $self = shift;
178 11 50       34 my $version = $self->schild(1) or return undef;
179 11 100       61 $version->isa('PPI::Token::Number') ? $version->content : '';
180             }
181              
182             =pod
183              
184             =head2 version_literal
185              
186             The C method has the same behavior as C, 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     11 1 742 my $self = shift;
194 11 50       37 my $version = $self->schild(1) or return undef;
195 11 100       72 $version->isa('PPI::Token::Number') ? $version->literal : '';
196             }
197              
198             =pod
199              
200             =head2 arguments
201              
202             The C 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 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     7 1 740 my $self = shift;
212 7         28 my @args = $self->schildren;
213              
214             # Remove the "use", "no" or "require"
215 7         14 shift @args;
216              
217             # Remove the statement terminator
218 7 100 66     51 if (
219             $args[-1]->isa('PPI::Token::Structure')
220             and
221             $args[-1]->content eq ';'
222             ) {
223 6         12 pop @args;
224             }
225              
226             # Remove the module or perl version.
227 7         8 shift @args;
228              
229 7 100       23 return unless @args;
230              
231 5 100       23 if ( $args[0]->isa('PPI::Token::Number') ) {
232 2 100       12 my $after = $args[1] or return;
233 1 50       7 $after->isa('PPI::Token::Operator') or shift @args;
234             }
235              
236 4         23 return @args;
237             }
238              
239             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 in the main module.
250              
251             =head1 AUTHOR
252              
253             Adam Kennedy Eadamk@cpan.orgE
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