File Coverage

blib/lib/PPI/Token/Comment.pm
Criterion Covered Total %
statement 16 21 76.1
branch 3 8 37.5
condition n/a
subroutine 4 6 66.6
pod 1 1 100.0
total 24 36 66.6


line stmt bran cond sub pod time code
1             package PPI::Token::Comment;
2              
3             =pod
4              
5             =head1 NAME
6              
7             PPI::Token::Comment - A comment in Perl source code
8              
9             =head1 INHERITANCE
10              
11             PPI::Token::Comment
12             isa PPI::Token
13             isa PPI::Element
14              
15             =head1 SYNOPSIS
16              
17             # This is a PPI::Token::Comment
18            
19             print "Hello World!"; # So it this
20            
21             $string =~ s/ foo # This, unfortunately, is not :(
22             bar
23             /w;
24              
25             =head1 DESCRIPTION
26              
27             In PPI, comments are represented by C objects.
28              
29             These come in two flavours, line comment and inline comments.
30              
31             A C is a comment that stands on its own line. These comments
32             hold their own newline and whitespace (both leading and trailing) as part
33             of the one C object.
34              
35             An inline comment is a comment that appears after some code, and
36             continues to the end of the line. This does B include whitespace,
37             and the terminating newlines is considered a separate
38             L token.
39              
40             This is largely a convenience, simplifying a lot of normal code relating
41             to the common things people do with comments.
42              
43             Most commonly, it means when you C or C a comment, a line
44             comment disappears taking the entire line with it, and an inline comment
45             is removed from the inside of the line, allowing the newline to drop
46             back onto the end of the code, as you would expect.
47              
48             It also means you can move comments around in blocks much more easily.
49              
50             For now, this is a suitably handy way to do things. However, I do reserve
51             the right to change my mind on this one if it gets dangerously
52             anachronistic somewhere down the line.
53              
54             =head1 METHODS
55              
56             Only very limited methods are available, beyond those provided by our
57             parent L and L classes.
58              
59             =cut
60              
61 64     64   363 use strict;
  64         108  
  64         1466  
62 64     64   261 use PPI::Token ();
  64         98  
  64         17285  
63              
64             our $VERSION = '1.275';
65              
66             our @ISA = "PPI::Token";
67              
68             ### XS -> PPI/XS.xs:_PPI_Token_Comment__significant 0.900+
69             sub significant() { '' }
70              
71             # Most stuff goes through __TOKENIZER__commit.
72             # This is such a rare case, do char at a time to keep the code small
73             sub __TOKENIZER__on_char {
74 0     0   0 my $t = $_[1];
75              
76             # Make sure not to include the trailing newline
77 0 0       0 if ( substr( $t->{line}, $t->{line_cursor}, 1 ) eq "\n" ) {
78 0         0 return $t->_finalize_token->__TOKENIZER__on_char( $t );
79             }
80              
81 0         0 1;
82             }
83              
84             sub __TOKENIZER__commit {
85 1581     1581   2194 my $t = $_[1];
86              
87             # Get the rest of the line
88 1581         2595 my $rest = substr( $t->{line}, $t->{line_cursor} );
89 1581 100       3210 if ( chomp $rest ) { # Include the newline separately
90             # Add the current token, and the newline
91 1220         2577 $t->_new_token('Comment', $rest);
92 1220         2196 $t->_new_token('Whitespace', "\n");
93             } else {
94             # Add this token only
95 361         700 $t->_new_token('Comment', $rest);
96             }
97              
98             # Advance the line cursor to the end
99 1581         2363 $t->{line_cursor} = $t->{line_length} - 1;
100              
101 1581         3597 0;
102             }
103              
104             # Comments end at the end of the line
105             sub __TOKENIZER__on_line_end {
106 361 50   361   1553 $_[1]->_finalize_token if $_[1]->{token};
107 361         426 1;
108             }
109              
110             =pod
111              
112             =head2 line
113              
114             The C accessor returns true if the C is a
115             line comment, or false if it is an inline comment.
116              
117             =cut
118              
119             sub line {
120             # Entire line comments have a newline at the end
121 0 0   0 1   $_[0]->{content} =~ /\n$/ ? 1 : 0;
122             }
123              
124             1;
125              
126             =pod
127              
128             =head1 SUPPORT
129              
130             See the L in the main module.
131              
132             =head1 AUTHOR
133              
134             Adam Kennedy Eadamk@cpan.orgE
135              
136             =head1 COPYRIGHT
137              
138             Copyright 2001 - 2011 Adam Kennedy.
139              
140             This program is free software; you can redistribute
141             it and/or modify it under the same terms as Perl itself.
142              
143             The full text of the license can be found in the
144             LICENSE file included with this module.
145              
146             =cut