File Coverage

blib/lib/PPI/Token/Prototype.pm
Criterion Covered Total %
statement 18 18 100.0
branch 3 4 75.0
condition n/a
subroutine 4 4 100.0
pod 1 1 100.0
total 26 27 96.3


line stmt bran cond sub pod time code
1             package PPI::Token::Prototype;
2              
3             =pod
4              
5             =head1 NAME
6              
7             PPI::Token::Prototype - A subroutine prototype descriptor
8              
9             =head1 INHERITANCE
10              
11             PPI::Token::End
12             isa PPI::Token
13             isa PPI::Element
14              
15             =head1 SYNOPSIS
16              
17             sub ($@) prototype;
18              
19             =head1 DESCRIPTION
20              
21             Although it sort of looks like a list or condition, a subroutine
22             prototype is a lot more like a string. Its job is to provide hints
23             to the perl compiler on what type of arguments a particular subroutine
24             expects, which the compiler uses to validate parameters at compile-time,
25             and allows programmers to use the functions without explicit parameter
26             parens.
27              
28             Due to the rise of OO Perl coding, which ignores these prototypes, they
29             are most often used to allow for constant-like things, and to "extend"
30             the language and create things that act like keywords and core functions.
31              
32             # Create something that acts like a constant
33             sub MYCONSTANT () { 10 }
34            
35             # Create the "any" core-looking function
36             sub any (&@) { ... }
37            
38             if ( any { $_->cute } @babies ) {
39             ...
40             }
41              
42             =head1 METHODS
43              
44             This class provides one additional method beyond those defined by the
45             L and L parent classes.
46              
47             =cut
48              
49 65     65   368 use strict;
  65         131  
  65         1529  
50 65     65   298 use PPI::Token ();
  65         100  
  65         14291  
51              
52             our $VERSION = '1.276';
53              
54             our @ISA = "PPI::Token";
55              
56             sub __TOKENIZER__on_char {
57 452     452   777 my $class = shift;
58 452         633 my $t = shift;
59              
60             # Suck in until we find the closing paren (or the end of line)
61 452         897 pos $t->{line} = $t->{line_cursor};
62 452 50       2164 die "regex should always match" if $t->{line} !~ m/\G(.*?\n?(?:\)|$))/gc;
63 452         1501 $t->{token}->{content} .= $1;
64 452         737 $t->{line_cursor} += length $1;
65              
66             # Shortcut if end of line
67 452 100       1580 return 0 unless $1 =~ /\)$/;
68              
69             # Found the closing paren
70 430         934 $t->_finalize_token->__TOKENIZER__on_char( $t );
71             }
72              
73             =pod
74              
75             =head2 prototype
76              
77             The C accessor returns the actual prototype pattern, stripped
78             of flanking parens and of all whitespace. This mirrors the behavior of
79             the Perl C builtin function.
80              
81             Note that stripping parens and whitespace means that the return of
82             C can be an empty string.
83              
84             =cut
85              
86             sub prototype {
87 196     196 1 53515 my $self = shift;
88 196         520 my $proto = $self->content;
89 196         1222 $proto =~ s/(^\(|\)$|\s+)//g;
90 196         687 $proto;
91             }
92              
93             1;
94              
95             =pod
96              
97             =head1 SUPPORT
98              
99             See the L in the main module.
100              
101             =head1 AUTHOR
102              
103             Adam Kennedy Eadamk@cpan.orgE
104              
105             =head1 COPYRIGHT
106              
107             Copyright 2001 - 2011 Adam Kennedy.
108              
109             This program is free software; you can redistribute
110             it and/or modify it under the same terms as Perl itself.
111              
112             The full text of the license can be found in the
113             LICENSE file included with this module.
114              
115             =cut