| 1 | package PPI::Document; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | PPI::Document - Object representation of a Perl document | ||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | =head1 INHERITANCE | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | PPI::Document | ||||||||||||||||||||||||||||||||||||||||||
| 12 | isa PPI::Node | ||||||||||||||||||||||||||||||||||||||||||
| 13 | isa PPI::Element | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | use PPI; | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | # Load a document from a file | ||||||||||||||||||||||||||||||||||||||||||
| 20 | my $Document = PPI::Document->new('My/Module.pm'); | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | # Strip out comments | ||||||||||||||||||||||||||||||||||||||||||
| 23 | $Document->prune('PPI::Token::Comment'); | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | # Find all the named subroutines | ||||||||||||||||||||||||||||||||||||||||||
| 26 | my $sub_nodes = $Document->find( | ||||||||||||||||||||||||||||||||||||||||||
| 27 | sub { $_[1]->isa('PPI::Statement::Sub') and $_[1]->name } | ||||||||||||||||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 29 | my @sub_names = map { $_->name } @$sub_nodes; | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | # Save the file | ||||||||||||||||||||||||||||||||||||||||||
| 32 | $Document->save('My/Module.pm.stripped'); | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | The C<PPI::Document> class represents a single Perl "document". A | ||||||||||||||||||||||||||||||||||||||||||
| 37 | C<PPI::Document> object acts as a root L<PPI::Node>, with some | ||||||||||||||||||||||||||||||||||||||||||
| 38 | additional methods for loading and saving, and working with | ||||||||||||||||||||||||||||||||||||||||||
| 39 | the line/column locations of Elements within a file. | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | The exemption to its L<PPI::Node>-like behavior this is that a | ||||||||||||||||||||||||||||||||||||||||||
| 42 | C<PPI::Document> object can NEVER have a parent node, and is always | ||||||||||||||||||||||||||||||||||||||||||
| 43 | the root node in a tree. | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | =head2 Storable Support | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | C<PPI::Document> implements the necessary C<STORABLE_freeze> and | ||||||||||||||||||||||||||||||||||||||||||
| 48 | C<STORABLE_thaw> hooks to provide native support for L<Storable>, | ||||||||||||||||||||||||||||||||||||||||||
| 49 | if you have it installed. | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | However if you want to clone a Document, you are highly recommended | ||||||||||||||||||||||||||||||||||||||||||
| 52 | to use the C<$Document-E<gt>clone> method rather than Storable's | ||||||||||||||||||||||||||||||||||||||||||
| 53 | C<dclone> function (although C<dclone> should still work). | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | Most of the things you are likely to want to do with a Document are | ||||||||||||||||||||||||||||||||||||||||||
| 58 | probably going to involve the methods from L<PPI::Node> class, of which | ||||||||||||||||||||||||||||||||||||||||||
| 59 | this is a subclass. | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | The methods listed here are the remaining few methods that are truly | ||||||||||||||||||||||||||||||||||||||||||
| 62 | Document-specific. | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 62 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 62 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 62 | ▶ | use List::Util 1.33 (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 62 | ▶ | use Params::Util qw{_SCALAR0 _ARRAY0 _INSTANCE}; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 62 | ▶ | use Digest::MD5 (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 62 | ▶ | use PPI::Util (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 62 | ▶ | use PPI (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 62 | ▶ | use PPI::Node (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 62 | ▶ | use overload 'bool' => \&PPI::Util::TRUE; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 62 | ▶ | use overload '""' => 'content'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 62 | ▶ | use vars qw{$VERSION @ISA $errstr}; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | BEGIN { | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 62 | ▶ | $VERSION = '1.236'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 62 | @ISA = 'PPI::Node'; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 62 | $errstr = ''; | |||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 62 | ▶ | use PPI::Document::Fragment (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | # Document cache | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 62 | my $CACHE; | |||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | # Convenience constants related to constants | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 62 | ▶ | use constant LOCATION_LINE => 0; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 62 | ▶ | use constant LOCATION_CHARACTER => 1; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 62 | ▶ | use constant LOCATION_COLUMN => 2; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 62 | ▶ | use constant LOCATION_LOGICAL_LINE => 3; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 62 | ▶ | use constant LOCATION_LOGICAL_FILE => 4; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | ##################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 102 | # Constructor and Static Methods | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | =head2 new | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | # Simple construction | ||||||||||||||||||||||||||||||||||||||||||
| 109 | $doc = PPI::Document->new( $filename ); | ||||||||||||||||||||||||||||||||||||||||||
| 110 | $doc = PPI::Document->new( \$source ); | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | # With the readonly attribute set | ||||||||||||||||||||||||||||||||||||||||||
| 113 | $doc = PPI::Document->new( $filename, | ||||||||||||||||||||||||||||||||||||||||||
| 114 | readonly => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 115 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | The C<new> constructor takes as argument a variety of different sources of | ||||||||||||||||||||||||||||||||||||||||||
| 118 | Perl code, and creates a single cohesive Perl C<PPI::Document> | ||||||||||||||||||||||||||||||||||||||||||
| 119 | for it. | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | If passed a file name as a normal string, it will attempt to load the | ||||||||||||||||||||||||||||||||||||||||||
| 122 | document from the file. | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | If passed a reference to a C<SCALAR>, this is taken to be source code and | ||||||||||||||||||||||||||||||||||||||||||
| 125 | parsed directly to create the document. | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | If passed zero arguments, a "blank" document will be created that contains | ||||||||||||||||||||||||||||||||||||||||||
| 128 | no content at all. | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | In all cases, the document is considered to be "anonymous" and not tied back | ||||||||||||||||||||||||||||||||||||||||||
| 131 | to where it was created from. Specifically, if you create a PPI::Document from | ||||||||||||||||||||||||||||||||||||||||||
| 132 | a filename, the document will B<not> remember where it was created from. | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | The constructor also takes attribute flags. | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | At this time, the only available attribute is the C<readonly> flag. | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | Setting C<readonly> to true will allow various systems to provide | ||||||||||||||||||||||||||||||||||||||||||
| 139 | additional optimisations and caching. Note that because C<readonly> is an | ||||||||||||||||||||||||||||||||||||||||||
| 140 | optimisation flag, it is off by default and you will need to explicitly | ||||||||||||||||||||||||||||||||||||||||||
| 141 | enable it. | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | Returns a C<PPI::Document> object, or C<undef> if parsing fails. | ||||||||||||||||||||||||||||||||||||||||||
| 144 | L<PPI::Exception> objects can also be thrown if there are parsing problems. | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | sub new { | ||||||||||||||||||||||||||||||||||||||||||
| 149 | 31318 | ▶ | local $_; # An extra one, just in case | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=18 SCAR=31.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | 31318 | ▶ | my $class = ref $_[0] ? ref shift : shift; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | 31318 | ▶ | unless ( @_ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | 15660 | my $self = $class->SUPER::new; | |||||||||||||||||||||||||||||||||||||||||
| 154 | 15660 | $self->{readonly} = ! 1; | |||||||||||||||||||||||||||||||||||||||||
| 155 | 15660 | $self->{tab_width} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 156 | 15660 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | # Check constructor attributes | ||||||||||||||||||||||||||||||||||||||||||
| 160 | 15658 | my $source = shift; | |||||||||||||||||||||||||||||||||||||||||
| 161 | 15658 | my %attr = @_; | |||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | # Check the data source | ||||||||||||||||||||||||||||||||||||||||||
| 164 | 15658 | ▶ | if ( ! defined $source ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | $class->_error("An undefined value was passed to PPI::Document::new"); | |||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | } elsif ( ! ref $source ) { | ||||||||||||||||||||||||||||||||||||||||||
| 168 | # Catch people using the old API | ||||||||||||||||||||||||||||||||||||||||||
| 169 | 471 | ▶ | if ( $source =~ /(?:\012|\015)/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | Carp::croak("API CHANGE: Source code should only be passed to PPI::Document->new as a SCALAR reference"); | |||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | # When loading from a filename, use the caching layer if it exists. | ||||||||||||||||||||||||||||||||||||||||||
| 174 | 471 | ▶ | if ( $CACHE ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | 3 | my $file_contents = PPI::Util::_slurp( $source ); | |||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | # Errors returned as plain string | ||||||||||||||||||||||||||||||||||||||||||
| 178 | 3 | ▶ | return $class->_error($file_contents) if !ref $file_contents; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | # Retrieve the document from the cache | ||||||||||||||||||||||||||||||||||||||||||
| 181 | 3 | my $document = $CACHE->get_document($file_contents); | |||||||||||||||||||||||||||||||||||||||||
| 182 | 3 | ▶ | return $class->_setattr( $document, %attr ) if $document; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | 1 | $document = PPI::Lexer->lex_source( $$file_contents ); | |||||||||||||||||||||||||||||||||||||||||
| 185 | 1 | ▶ | if ( $document ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | # Save in the cache | ||||||||||||||||||||||||||||||||||||||||||
| 187 | 1 | $CACHE->store_document( $document ); | |||||||||||||||||||||||||||||||||||||||||
| 188 | 1 | return $class->_setattr( $document, %attr ); | |||||||||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||||||||
| 190 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 191 | 468 | my $document = PPI::Lexer->lex_file( $source ); | |||||||||||||||||||||||||||||||||||||||||
| 192 | 468 | ▶ | return $class->_setattr( $document, %attr ) if $document; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | } elsif ( _SCALAR0($source) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 196 | 15184 | my $document = PPI::Lexer->lex_source( $$source ); | |||||||||||||||||||||||||||||||||||||||||
| 197 | 15184 | ▶ | return $class->_setattr( $document, %attr ) if $document; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | } elsif ( _ARRAY0($source) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 200 | 3 | $source = join '', map { "$_\n" } @$source; | |||||||||||||||||||||||||||||||||||||||||
| 201 | 3 | my $document = PPI::Lexer->lex_source( $source ); | |||||||||||||||||||||||||||||||||||||||||
| 202 | 3 | ▶ | return $class->_setattr( $document, %attr ) if $document; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | $class->_error("Unknown object or reference was passed to PPI::Document::new"); | |||||||||||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | # Pull and store the error from the lexer | ||||||||||||||||||||||||||||||||||||||||||
| 209 | 1 | my $errstr; | |||||||||||||||||||||||||||||||||||||||||
| 210 | 1 | ▶ | if ( _INSTANCE($@, 'PPI::Exception') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 211 | 1 | $errstr = $@->message; | |||||||||||||||||||||||||||||||||||||||||
| 212 | } elsif ( $@ ) { | ||||||||||||||||||||||||||||||||||||||||||
| 213 | 0 | $errstr = $@; | |||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | $errstr =~ s/\sat line\s.+$//; | |||||||||||||||||||||||||||||||||||||||||
| 215 | } elsif ( PPI::Lexer->errstr ) { | ||||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | $errstr = PPI::Lexer->errstr; | |||||||||||||||||||||||||||||||||||||||||
| 217 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | $errstr = "Unknown error parsing Perl document"; | |||||||||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||||||||||
| 220 | 1 | PPI::Lexer->_clear; | |||||||||||||||||||||||||||||||||||||||||
| 221 | 1 | $class->_error( $errstr ); | |||||||||||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | sub load { | ||||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | ▶ | Carp::croak("API CHANGE: File names should now be passed to PPI::Document->new to load a file"); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
load: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | sub _setattr { | ||||||||||||||||||||||||||||||||||||||||||
| 229 | 15657 | ▶ | my ($class, $document, %attr) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_setattr: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | 15657 | $document->{readonly} = !! $attr{readonly}; | |||||||||||||||||||||||||||||||||||||||||
| 231 | 15657 | return $document; | |||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | =head2 set_cache $cache | ||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||
| 238 | As of L<PPI> 1.100, C<PPI::Document> supports parser caching. | ||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | The default cache class L<PPI::Cache> provides a L<Storable>-based | ||||||||||||||||||||||||||||||||||||||||||
| 241 | caching or the parsed document based on the MD5 hash of the document as | ||||||||||||||||||||||||||||||||||||||||||
| 242 | a string. | ||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | The static C<set_cache> method is used to set the cache object for | ||||||||||||||||||||||||||||||||||||||||||
| 245 | C<PPI::Document> to use when loading documents. It takes as argument | ||||||||||||||||||||||||||||||||||||||||||
| 246 | a L<PPI::Cache> object (or something that C<isa> the same). | ||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | If passed C<undef>, this method will stop using the current cache, if any. | ||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||
| 250 | For more information on caching, see L<PPI::Cache>. | ||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||
| 252 | Returns true on success, or C<undef> if not passed a valid param. | ||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||
| 256 | sub set_cache { | ||||||||||||||||||||||||||||||||||||||||||
| 257 | 3 | ▶ | my $class = ref $_[0] ? ref shift : shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_cache: called CC=4 SCAR=14.0
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | 3 | ▶ | if ( defined $_[0] ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 260 | # Enable the cache | ||||||||||||||||||||||||||||||||||||||||||
| 261 | 2 | ▶ | my $object = _INSTANCE(shift, 'PPI::Cache') or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 262 | 2 | $CACHE = $object; | |||||||||||||||||||||||||||||||||||||||||
| 263 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 264 | # Disable the cache | ||||||||||||||||||||||||||||||||||||||||||
| 265 | 1 | $CACHE = undef; | |||||||||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||
| 268 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||
| 273 | =head2 get_cache | ||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||
| 275 | If a document cache is currently set, the C<get_cache> method will | ||||||||||||||||||||||||||||||||||||||||||
| 276 | return it. | ||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||
| 278 | Returns a L<PPI::Cache> object, or C<undef> if there is no cache | ||||||||||||||||||||||||||||||||||||||||||
| 279 | currently set for C<PPI::Document>. | ||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||
| 281 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | sub get_cache { | ||||||||||||||||||||||||||||||||||||||||||
| 284 | 7 | ▶ | $CACHE; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_cache: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||
| 291 | ##################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 292 | # PPI::Document Instance Methods | ||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||
| 294 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||
| 296 | =head2 readonly | ||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | The C<readonly> attribute indicates if the document is intended to be | ||||||||||||||||||||||||||||||||||||||||||
| 299 | read-only, and will never be modified. This is an advisory flag, that | ||||||||||||||||||||||||||||||||||||||||||
| 300 | writers of L<PPI>-related systems may or may not use to enable | ||||||||||||||||||||||||||||||||||||||||||
| 301 | optimisations and caches for your document. | ||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||
| 303 | Returns true if the document is read-only or false if not. | ||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||
| 305 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||
| 307 | sub readonly { | ||||||||||||||||||||||||||||||||||||||||||
| 308 | 4 | ▶ | $_[0]->{readonly}; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
readonly: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||
| 311 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||
| 313 | =head2 tab_width [ $width ] | ||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | In order to handle support for C<location> correctly, C<Documents> | ||||||||||||||||||||||||||||||||||||||||||
| 316 | need to understand the concept of tabs and tab width. The C<tab_width> | ||||||||||||||||||||||||||||||||||||||||||
| 317 | method is used to get and set the size of the tab width. | ||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||
| 319 | At the present time, PPI only supports "naive" (width 1) tabs, but we do | ||||||||||||||||||||||||||||||||||||||||||
| 320 | plan on supporting arbitrary, default and auto-sensing tab widths later. | ||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||
| 322 | Returns the tab width as an integer, or C<die>s if you attempt to set the | ||||||||||||||||||||||||||||||||||||||||||
| 323 | tab width. | ||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||
| 325 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||
| 327 | sub tab_width { | ||||||||||||||||||||||||||||||||||||||||||
| 328 | 53964 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tab_width: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 329 | 53964 | ▶ | return $self->{tab_width} unless @_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 330 | 2 | $self->{tab_width} = shift; | |||||||||||||||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||
| 335 | =head2 save | ||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | $document->save( $file ) | ||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||
| 339 | The C<save> method serializes the C<PPI::Document> object and saves the | ||||||||||||||||||||||||||||||||||||||||||
| 340 | resulting Perl document to a file. Returns C<undef> on failure to open | ||||||||||||||||||||||||||||||||||||||||||
| 341 | or write to the file. | ||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||
| 343 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||||||||||||
| 345 | sub save { | ||||||||||||||||||||||||||||||||||||||||||
| 346 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
save: called CC=4 SCAR=14.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 347 | 2 | local *FILE; | |||||||||||||||||||||||||||||||||||||||||
| 348 | 2 | ▶ | open( FILE, '>', $_[0] ) or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 349 | 2 | binmode FILE; | |||||||||||||||||||||||||||||||||||||||||
| 350 | 2 | ▶ | print FILE $self->serialize or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 351 | 2 | ▶ | close FILE or return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 352 | 2 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||
| 355 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||
| 357 | =head2 serialize | ||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||
| 359 | Unlike the C<content> method, which shows only the immediate content | ||||||||||||||||||||||||||||||||||||||||||
| 360 | within an element, Document objects also have to be able to be written | ||||||||||||||||||||||||||||||||||||||||||
| 361 | out to a file again. | ||||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||
| 363 | When doing this we need to take into account some additional factors. | ||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||
| 365 | Primarily, we need to handle here-docs correctly, so that are written | ||||||||||||||||||||||||||||||||||||||||||
| 366 | to the file in the expected place. | ||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||
| 368 | The C<serialize> method generates the actual file content for a given | ||||||||||||||||||||||||||||||||||||||||||
| 369 | Document object. The resulting string can be written straight to a file. | ||||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||
| 371 | Returns the serialized document as a string. | ||||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||
| 373 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||
| 375 | sub serialize { | ||||||||||||||||||||||||||||||||||||||||||
| 376 | 6056 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
serialize: called CC=18 SCAR=32.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 377 | 6056 | my @tokens = $self->tokens; | |||||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||||||
| 379 | # The here-doc content buffer | ||||||||||||||||||||||||||||||||||||||||||
| 380 | 6056 | my $heredoc = ''; | |||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||
| 382 | # Start the main loop | ||||||||||||||||||||||||||||||||||||||||||
| 383 | 6056 | my $output = ''; | |||||||||||||||||||||||||||||||||||||||||
| 384 | 6056 | foreach my $i ( 0 .. $#tokens ) { | |||||||||||||||||||||||||||||||||||||||||
| 385 | 239498 | my $Token = $tokens[$i]; | |||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||
| 387 | # Handle normal tokens | ||||||||||||||||||||||||||||||||||||||||||
| 388 | 239498 | ▶ | unless ( $Token->isa('PPI::Token::HereDoc') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 389 | 239316 | my $content = $Token->content; | |||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||
| 391 | # Handle the trivial cases | ||||||||||||||||||||||||||||||||||||||||||
| 392 | 239316 | ▶ | unless ( $heredoc ne '' and $content =~ /\n/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$heredoc ne "" and $content =~ /\n/
MC/DC100%2/2
$heredoc ne "" and $content =~ /\n/
$heredoc ne ""
$content =~ /\n/
Truth table100%3/3
A and B
A= $heredoc ne ""B= $content =~ /\n/
| |||||||||||||||||||||||||||||||||||||||||||
| 393 | 239167 | $output .= $content; | |||||||||||||||||||||||||||||||||||||||||
| 394 | 239167 | next; | |||||||||||||||||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||
| 397 | # We have pending here-doc content that needs to be | ||||||||||||||||||||||||||||||||||||||||||
| 398 | # inserted just after the first newline in the content. | ||||||||||||||||||||||||||||||||||||||||||
| 399 | 149 | ▶ | if ( $content eq "\n" ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 400 | # Shortcut the most common case for speed | ||||||||||||||||||||||||||||||||||||||||||
| 401 | 145 | $output .= $content . $heredoc; | |||||||||||||||||||||||||||||||||||||||||
| 402 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 403 | # Slower and more general version | ||||||||||||||||||||||||||||||||||||||||||
| 404 | 4 | $content =~ s/\n/\n$heredoc/; | |||||||||||||||||||||||||||||||||||||||||
| 405 | 4 | $output .= $content; | |||||||||||||||||||||||||||||||||||||||||
| 406 | } | ||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||
| 408 | 149 | $heredoc = ''; | |||||||||||||||||||||||||||||||||||||||||
| 409 | 149 | next; | |||||||||||||||||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||
| 412 | # This token is a HereDoc. | ||||||||||||||||||||||||||||||||||||||||||
| 413 | # First, add the token content as normal, which in this | ||||||||||||||||||||||||||||||||||||||||||
| 414 | # case will definitely not contain a newline. | ||||||||||||||||||||||||||||||||||||||||||
| 415 | 182 | $output .= $Token->content; | |||||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||||||
| 417 | # Now add all of the here-doc content to the heredoc buffer. | ||||||||||||||||||||||||||||||||||||||||||
| 418 | 182 | foreach my $line ( $Token->heredoc ) { | |||||||||||||||||||||||||||||||||||||||||
| 419 | 453 | $heredoc .= $line; | |||||||||||||||||||||||||||||||||||||||||
| 420 | } | ||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||
| 422 | 182 | ▶ | if ( $Token->{_damaged} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 423 | # Special Case: | ||||||||||||||||||||||||||||||||||||||||||
| 424 | # There are a couple of warning/bug situations | ||||||||||||||||||||||||||||||||||||||||||
| 425 | # that can occur when a HereDoc content was read in | ||||||||||||||||||||||||||||||||||||||||||
| 426 | # from the end of a file that we silently allow. | ||||||||||||||||||||||||||||||||||||||||||
| 427 | # | ||||||||||||||||||||||||||||||||||||||||||
| 428 | # When writing back out to the file we have to | ||||||||||||||||||||||||||||||||||||||||||
| 429 | # auto-repair these problems if we aren't going back | ||||||||||||||||||||||||||||||||||||||||||
| 430 | # on to the end of the file. | ||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||
| 432 | # When calculating $last_line, ignore the final token if | ||||||||||||||||||||||||||||||||||||||||||
| 433 | # and only if it has a single newline at the end. | ||||||||||||||||||||||||||||||||||||||||||
| 434 | 56 | my $last_index = $#tokens; | |||||||||||||||||||||||||||||||||||||||||
| 435 | 56 | ▶ | if ( $tokens[$last_index]->{content} =~ /^[^\n]*\n$/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 436 | 31 | $last_index--; | |||||||||||||||||||||||||||||||||||||||||
| 437 | } | ||||||||||||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||||||||||||
| 439 | # This is a two part test. | ||||||||||||||||||||||||||||||||||||||||||
| 440 | # First, are we on the last line of the | ||||||||||||||||||||||||||||||||||||||||||
| 441 | # content part of the file | ||||||||||||||||||||||||||||||||||||||||||
| 442 | my $last_line = List::Util::none { | ||||||||||||||||||||||||||||||||||||||||||
| 443 | 82 | ▶ | $tokens[$_] and $tokens[$_]->{content} =~ /\n/ | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 444 | 56 | } (($i + 1) .. $last_index); | |||||||||||||||||||||||||||||||||||||||||
| 445 | 56 | ▶ | if ( ! defined $last_line ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 446 | # Handles the null list case | ||||||||||||||||||||||||||||||||||||||||||
| 447 | 0 | $last_line = 1; | |||||||||||||||||||||||||||||||||||||||||
| 448 | } | ||||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||
| 450 | # Secondly, are their any more here-docs after us, | ||||||||||||||||||||||||||||||||||||||||||
| 451 | # (with content or a terminator) | ||||||||||||||||||||||||||||||||||||||||||
| 452 | my $any_after = List::Util::any { | ||||||||||||||||||||||||||||||||||||||||||
| 453 | $tokens[$_]->isa('PPI::Token::HereDoc') | ||||||||||||||||||||||||||||||||||||||||||
| 454 | and ( | ||||||||||||||||||||||||||||||||||||||||||
| 455 | 2 | scalar(@{$tokens[$_]->{_heredoc}}) | |||||||||||||||||||||||||||||||||||||||||
| 456 | or | ||||||||||||||||||||||||||||||||||||||||||
| 457 | defined $tokens[$_]->{_terminator_line} | ||||||||||||||||||||||||||||||||||||||||||
| 458 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 459 | 56 | ▶ | } (($i + 1) .. $#tokens); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.8
Branch: 1 branch
Condition50%3/6
$tokens[$_]->isa('PPI::Token::HereDoc') && (scalar @{$tokens[$_]{'_heredoc'};} || defined $tokens[$_]{'_terminator_line'})
scalar @{$tokens[$_]{"_heredoc"};} or defined $tokens[$_]{"_terminator_line"}
MC/DC0%0/3
$tokens[$_]->isa('PPI::Token::HereDoc') && (scalar @{$tokens[$_]{'_heredoc'};} || defined $tokens[$_]{'_terminator_line'})
$tokens[$_]->isa('PPI::Token::HereDoc')
scalar @{$tokens[$_]{"_heredoc"};}
defined $tokens[$_]{"_terminator_line"}
Truth table50%2/4
A && B or C
A= $tokens[$_]->isa('PPI::Token::HereDoc')B= scalar @{$tokens[$_]{"_heredoc"};}C= defined $tokens[$_]{"_terminator_line"}
| |||||||||||||||||||||||||||||||||||||||||||
| 460 | 56 | ▶ | if ( ! defined $any_after ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 461 | # Handles the null list case | ||||||||||||||||||||||||||||||||||||||||||
| 462 | 0 | $any_after = ''; | |||||||||||||||||||||||||||||||||||||||||
| 463 | } | ||||||||||||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||||||||||||
| 465 | # We don't need to repair the last here-doc on the | ||||||||||||||||||||||||||||||||||||||||||
| 466 | # last line. But we do need to repair anything else. | ||||||||||||||||||||||||||||||||||||||||||
| 467 | 56 | ▶ | unless ( $last_line and ! $any_after ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$last_line and not $any_after
MC/DC0%0/2
$last_line and not $any_after
$last_line
not $any_after
Truth table33%1/3
A and not(B)
A= $last_lineB= not $any_after
| |||||||||||||||||||||||||||||||||||||||||||
| 468 | # Add a terminating string if it didn't have one | ||||||||||||||||||||||||||||||||||||||||||
| 469 | 0 | ▶ | unless ( defined $Token->{_terminator_line} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 470 | 0 | $Token->{_terminator_line} = $Token->{_terminator}; | |||||||||||||||||||||||||||||||||||||||||
| 471 | } | ||||||||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||||||
| 473 | # Add a trailing newline to the terminating | ||||||||||||||||||||||||||||||||||||||||||
| 474 | # string if it didn't have one. | ||||||||||||||||||||||||||||||||||||||||||
| 475 | 0 | ▶ | unless ( $Token->{_terminator_line} =~ /\n$/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 476 | 0 | $Token->{_terminator_line} .= "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 477 | } | ||||||||||||||||||||||||||||||||||||||||||
| 478 | } | ||||||||||||||||||||||||||||||||||||||||||
| 479 | } | ||||||||||||||||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||||||||||||||||
| 481 | # Now add the termination line to the heredoc buffer | ||||||||||||||||||||||||||||||||||||||||||
| 482 | 182 | ▶ | if ( defined $Token->{_terminator_line} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 483 | 133 | $heredoc .= $Token->{_terminator_line}; | |||||||||||||||||||||||||||||||||||||||||
| 484 | } | ||||||||||||||||||||||||||||||||||||||||||
| 485 | } | ||||||||||||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||||||||||||
| 487 | # End of tokens | ||||||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||||||
| 489 | 6056 | ▶ | if ( $heredoc ne '' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 490 | # If the file doesn't end in a newline, we need to add one | ||||||||||||||||||||||||||||||||||||||||||
| 491 | # so that the here-doc content starts on the next line. | ||||||||||||||||||||||||||||||||||||||||||
| 492 | 0 | ▶ | unless ( $output =~ /\n$/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 493 | 0 | $output .= "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 494 | } | ||||||||||||||||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||||||||||||||||
| 496 | # Now we add the remaining here-doc content | ||||||||||||||||||||||||||||||||||||||||||
| 497 | # to the end of the file. | ||||||||||||||||||||||||||||||||||||||||||
| 498 | 0 | $output .= $heredoc; | |||||||||||||||||||||||||||||||||||||||||
| 499 | } | ||||||||||||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||||||||||||
| 501 | 6056 | $output; | |||||||||||||||||||||||||||||||||||||||||
| 502 | } | ||||||||||||||||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||||||||||||||||
| 504 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||||||||||||||||
| 506 | =head2 hex_id | ||||||||||||||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||||||||||||
| 508 | The C<hex_id> method generates an unique identifier for the Perl document. | ||||||||||||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||||||||||||
| 510 | This identifier is basically just the serialized document, with | ||||||||||||||||||||||||||||||||||||||||||
| 511 | Unix-specific newlines, passed through MD5 to produce a hexadecimal string. | ||||||||||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||||||||||
| 513 | This identifier is used by a variety of systems (such as L<PPI::Cache> | ||||||||||||||||||||||||||||||||||||||||||
| 514 | and L<Perl::Metrics>) as a unique key against which to store or cache | ||||||||||||||||||||||||||||||||||||||||||
| 515 | information about a document (or indeed, to cache the document itself). | ||||||||||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||||||||||
| 517 | Returns a 32 character hexadecimal string. | ||||||||||||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||||||||||||
| 519 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||||||||||||||||
| 521 | sub hex_id { | ||||||||||||||||||||||||||||||||||||||||||
| 522 | 156 | ▶ | PPI::Util::md5hex($_[0]->serialize); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
hex_id: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 523 | } | ||||||||||||||||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||||||||||||||||
| 525 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||||||||||||||||
| 527 | =head2 index_locations | ||||||||||||||||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||||||||||||||||
| 529 | Within a document, all L<PPI::Element> objects can be considered to have a | ||||||||||||||||||||||||||||||||||||||||||
| 530 | "location", a line/column position within the document when considered as a | ||||||||||||||||||||||||||||||||||||||||||
| 531 | file. This position is primarily useful for debugging type activities. | ||||||||||||||||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||||||||||||||||
| 533 | The method for finding the position of a single Element is a bit laborious, | ||||||||||||||||||||||||||||||||||||||||||
| 534 | and very slow if you need to do it a lot. So the C<index_locations> method | ||||||||||||||||||||||||||||||||||||||||||
| 535 | will index and save the locations of every Element within the Document in | ||||||||||||||||||||||||||||||||||||||||||
| 536 | advance, making future calls to <PPI::Element::location> virtually free. | ||||||||||||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||||||||||||
| 538 | Please note that this index should always be cleared using C<flush_locations> | ||||||||||||||||||||||||||||||||||||||||||
| 539 | once you are finished with the locations. If content is added to or removed | ||||||||||||||||||||||||||||||||||||||||||
| 540 | from the file, these indexed locations will be B<wrong>. | ||||||||||||||||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||||||||||||||||
| 542 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||||||||||||
| 544 | sub index_locations { | ||||||||||||||||||||||||||||||||||||||||||
| 545 | 237 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
index_locations: called CC=8 SCAR=21.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 546 | 237 | my @tokens = $self->tokens; | |||||||||||||||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||||||||||||||||
| 548 | # Whenever we hit a heredoc we will need to increment by | ||||||||||||||||||||||||||||||||||||||||||
| 549 | # the number of lines in it's content section when we | ||||||||||||||||||||||||||||||||||||||||||
| 550 | # encounter the next token with a newline in it. | ||||||||||||||||||||||||||||||||||||||||||
| 551 | 237 | my $heredoc = 0; | |||||||||||||||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||||||||||||||||
| 553 | # Find the first Token without a location | ||||||||||||||||||||||||||||||||||||||||||
| 554 | 237 | my ($first, $location) = (); | |||||||||||||||||||||||||||||||||||||||||
| 555 | 237 | foreach ( 0 .. $#tokens ) { | |||||||||||||||||||||||||||||||||||||||||
| 556 | 236 | my $Token = $tokens[$_]; | |||||||||||||||||||||||||||||||||||||||||
| 557 | 236 | ▶ | next if $Token->{_location}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||||||||||||||||
| 559 | # Found the first Token without a location | ||||||||||||||||||||||||||||||||||||||||||
| 560 | # Calculate the new location if needed. | ||||||||||||||||||||||||||||||||||||||||||
| 561 | 236 | ▶ | if ($_) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 562 | 0 | $location = | |||||||||||||||||||||||||||||||||||||||||
| 563 | $self->_add_location( $location, $tokens[$_ - 1], \$heredoc ); | ||||||||||||||||||||||||||||||||||||||||||
| 564 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 565 | 236 | ▶ | my $logical_file = | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 566 | $self->can('filename') ? $self->filename : undef; | ||||||||||||||||||||||||||||||||||||||||||
| 567 | 236 | $location = [ 1, 1, 1, 1, $logical_file ]; | |||||||||||||||||||||||||||||||||||||||||
| 568 | } | ||||||||||||||||||||||||||||||||||||||||||
| 569 | 236 | $first = $_; | |||||||||||||||||||||||||||||||||||||||||
| 570 | 236 | last; | |||||||||||||||||||||||||||||||||||||||||
| 571 | } | ||||||||||||||||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||||||||||||||||
| 573 | # Calculate locations for the rest | ||||||||||||||||||||||||||||||||||||||||||
| 574 | 237 | ▶ | if ( defined $first ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 575 | 236 | foreach ( $first .. $#tokens ) { | |||||||||||||||||||||||||||||||||||||||||
| 576 | 64487 | my $Token = $tokens[$_]; | |||||||||||||||||||||||||||||||||||||||||
| 577 | 64487 | $Token->{_location} = $location; | |||||||||||||||||||||||||||||||||||||||||
| 578 | 64487 | $location = $self->_add_location( $location, $Token, \$heredoc ); | |||||||||||||||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||||||||||||||||
| 580 | # Add any here-doc lines to the counter | ||||||||||||||||||||||||||||||||||||||||||
| 581 | 64487 | ▶ | if ( $Token->isa('PPI::Token::HereDoc') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 582 | 26 | $heredoc += $Token->heredoc + 1; | |||||||||||||||||||||||||||||||||||||||||
| 583 | } | ||||||||||||||||||||||||||||||||||||||||||
| 584 | } | ||||||||||||||||||||||||||||||||||||||||||
| 585 | } | ||||||||||||||||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||||||||||||||||
| 587 | 237 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 588 | } | ||||||||||||||||||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||||||||||||||||
| 590 | sub _add_location { | ||||||||||||||||||||||||||||||||||||||||||
| 591 | 64487 | ▶ | my ($self, $start, $Token, $heredoc) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_add_location: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 592 | 64487 | my $content = $Token->{content}; | |||||||||||||||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||||||||||||||||
| 594 | # Does the content contain any newlines | ||||||||||||||||||||||||||||||||||||||||||
| 595 | 64487 | my $newlines =()= $content =~ /\n/g; | |||||||||||||||||||||||||||||||||||||||||
| 596 | 64487 | my ($logical_line, $logical_file) = | |||||||||||||||||||||||||||||||||||||||||
| 597 | $self->_logical_line_and_file($start, $Token, $newlines); | ||||||||||||||||||||||||||||||||||||||||||
| 598 | |||||||||||||||||||||||||||||||||||||||||||
| 599 | 64487 | ▶ | unless ( $newlines ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 600 | # Handle the simple case | ||||||||||||||||||||||||||||||||||||||||||
| 601 | return [ | ||||||||||||||||||||||||||||||||||||||||||
| 602 | 53701 | $start->[LOCATION_LINE], | |||||||||||||||||||||||||||||||||||||||||
| 603 | $start->[LOCATION_CHARACTER] + length($content), | ||||||||||||||||||||||||||||||||||||||||||
| 604 | $start->[LOCATION_COLUMN] | ||||||||||||||||||||||||||||||||||||||||||
| 605 | + $self->_visual_length( | ||||||||||||||||||||||||||||||||||||||||||
| 606 | $content, | ||||||||||||||||||||||||||||||||||||||||||
| 607 | $start->[LOCATION_COLUMN] | ||||||||||||||||||||||||||||||||||||||||||
| 608 | ), | ||||||||||||||||||||||||||||||||||||||||||
| 609 | $logical_line, | ||||||||||||||||||||||||||||||||||||||||||
| 610 | $logical_file, | ||||||||||||||||||||||||||||||||||||||||||
| 611 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 612 | } | ||||||||||||||||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||||||||||||||||
| 614 | # This is the more complex case where we hit or | ||||||||||||||||||||||||||||||||||||||||||
| 615 | # span a newline boundary. | ||||||||||||||||||||||||||||||||||||||||||
| 616 | 10786 | my $physical_line = $start->[LOCATION_LINE] + $newlines; | |||||||||||||||||||||||||||||||||||||||||
| 617 | 10786 | my $location = [ $physical_line, 1, 1, $logical_line, $logical_file ]; | |||||||||||||||||||||||||||||||||||||||||
| 618 | 10786 | ▶ | if ( $heredoc and $$heredoc ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$heredoc and $$heredoc
MC/DC50%1/2
$heredoc and $$heredoc
$heredoc
$$heredoc
Truth table66%2/3
A and B
A= $heredocB= $$heredoc
| |||||||||||||||||||||||||||||||||||||||||||
| 619 | 23 | $location->[LOCATION_LINE] += $$heredoc; | |||||||||||||||||||||||||||||||||||||||||
| 620 | 23 | $location->[LOCATION_LOGICAL_LINE] += $$heredoc; | |||||||||||||||||||||||||||||||||||||||||
| 621 | 23 | $$heredoc = 0; | |||||||||||||||||||||||||||||||||||||||||
| 622 | } | ||||||||||||||||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||||||||||||||||
| 624 | # Does the token have additional characters | ||||||||||||||||||||||||||||||||||||||||||
| 625 | # after their last newline. | ||||||||||||||||||||||||||||||||||||||||||
| 626 | 10786 | ▶ | if ( $content =~ /\n([^\n]+?)\z/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 627 | 260 | $location->[LOCATION_CHARACTER] += length($1); | |||||||||||||||||||||||||||||||||||||||||
| 628 | 260 | $location->[LOCATION_COLUMN] += | |||||||||||||||||||||||||||||||||||||||||
| 629 | $self->_visual_length( | ||||||||||||||||||||||||||||||||||||||||||
| 630 | $1, $location->[LOCATION_COLUMN], | ||||||||||||||||||||||||||||||||||||||||||
| 631 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 632 | } | ||||||||||||||||||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||||||||||||||||
| 634 | 10786 | $location; | |||||||||||||||||||||||||||||||||||||||||
| 635 | } | ||||||||||||||||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||||||||||||||||
| 637 | sub _logical_line_and_file { | ||||||||||||||||||||||||||||||||||||||||||
| 638 | 64487 | ▶ | my ($self, $start, $Token, $newlines) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_logical_line_and_file: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||||||||||||||||
| 640 | # Regex taken from perlsyn, with the correction that there's no space | ||||||||||||||||||||||||||||||||||||||||||
| 641 | # required between the line number and the file name. | ||||||||||||||||||||||||||||||||||||||||||
| 642 | 64487 | ▶ | if ($start->[LOCATION_CHARACTER] == 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 643 | 10552 | ▶ | if ( $Token->isa('PPI::Token::Comment') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 644 | 1733 | ▶ | if ( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 645 | $Token->content =~ m< | ||||||||||||||||||||||||||||||||||||||||||
| 646 | \A | ||||||||||||||||||||||||||||||||||||||||||
| 647 | \# \s* | ||||||||||||||||||||||||||||||||||||||||||
| 648 | line \s+ | ||||||||||||||||||||||||||||||||||||||||||
| 649 | (\d+) \s* | ||||||||||||||||||||||||||||||||||||||||||
| 650 | (?: (\"?) ([^\"]* [^\s\"]) \2 )? | ||||||||||||||||||||||||||||||||||||||||||
| 651 | \s* | ||||||||||||||||||||||||||||||||||||||||||
| 652 | \z | ||||||||||||||||||||||||||||||||||||||||||
| 653 | >xms | ||||||||||||||||||||||||||||||||||||||||||
| 654 | ) { | ||||||||||||||||||||||||||||||||||||||||||
| 655 | 8 | ▶ | return $1, ($3 || $start->[LOCATION_LOGICAL_FILE]); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$3 || $start->[4]
MC/DC50%1/2
$3 || $start->[4]
$3
$start->[4]
Truth table66%2/3
A || B
A= $3B= $start->[4]
| |||||||||||||||||||||||||||||||||||||||||||
| 656 | } | ||||||||||||||||||||||||||||||||||||||||||
| 657 | } | ||||||||||||||||||||||||||||||||||||||||||
| 658 | elsif ( $Token->isa('PPI::Token::Pod') ) { | ||||||||||||||||||||||||||||||||||||||||||
| 659 | 341 | my $content = $Token->content; | |||||||||||||||||||||||||||||||||||||||||
| 660 | 341 | my $line; | |||||||||||||||||||||||||||||||||||||||||
| 661 | 341 | my $file = $start->[LOCATION_LOGICAL_FILE]; | |||||||||||||||||||||||||||||||||||||||||
| 662 | 341 | my $end_of_directive; | |||||||||||||||||||||||||||||||||||||||||
| 663 | 341 | ▶ | while ( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 664 | $content =~ m< | ||||||||||||||||||||||||||||||||||||||||||
| 665 | ^ | ||||||||||||||||||||||||||||||||||||||||||
| 666 | \# \s*? | ||||||||||||||||||||||||||||||||||||||||||
| 667 | line \s+? | ||||||||||||||||||||||||||||||||||||||||||
| 668 | (\d+) (?: (?! \n) \s)* | ||||||||||||||||||||||||||||||||||||||||||
| 669 | (?: (\"?) ([^\"]*? [^\s\"]) \2 )?? | ||||||||||||||||||||||||||||||||||||||||||
| 670 | \s*? | ||||||||||||||||||||||||||||||||||||||||||
| 671 | $ | ||||||||||||||||||||||||||||||||||||||||||
| 672 | >xmsg | ||||||||||||||||||||||||||||||||||||||||||
| 673 | ) { | ||||||||||||||||||||||||||||||||||||||||||
| 674 | 6 | ▶ | ($line, $file) = ($1, ( $3 || $file ) ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$3 || $file
MC/DC0%0/2
$3 || $file
$3
$file
Truth table66%2/3
A || B
A= $3B= $file
| |||||||||||||||||||||||||||||||||||||||||||
| 675 | 6 | $end_of_directive = pos $content; | |||||||||||||||||||||||||||||||||||||||||
| 676 | } | ||||||||||||||||||||||||||||||||||||||||||
| 677 | |||||||||||||||||||||||||||||||||||||||||||
| 678 | 341 | ▶ | if (defined $line) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 679 | 6 | pos $content = $end_of_directive; | |||||||||||||||||||||||||||||||||||||||||
| 680 | 6 | my $post_directive_newlines =()= $content =~ m< \G [^\n]* \n >xmsg; | |||||||||||||||||||||||||||||||||||||||||
| 681 | 6 | return $line + $post_directive_newlines - 1, $file; | |||||||||||||||||||||||||||||||||||||||||
| 682 | } | ||||||||||||||||||||||||||||||||||||||||||
| 683 | } | ||||||||||||||||||||||||||||||||||||||||||
| 684 | } | ||||||||||||||||||||||||||||||||||||||||||
| 685 | |||||||||||||||||||||||||||||||||||||||||||
| 686 | return | ||||||||||||||||||||||||||||||||||||||||||
| 687 | 64473 | $start->[LOCATION_LOGICAL_LINE] + $newlines, | |||||||||||||||||||||||||||||||||||||||||
| 688 | $start->[LOCATION_LOGICAL_FILE]; | ||||||||||||||||||||||||||||||||||||||||||
| 689 | } | ||||||||||||||||||||||||||||||||||||||||||
| 690 | |||||||||||||||||||||||||||||||||||||||||||
| 691 | sub _visual_length { | ||||||||||||||||||||||||||||||||||||||||||
| 692 | 53961 | ▶ | my ($self, $content, $pos) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_visual_length: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 693 | |||||||||||||||||||||||||||||||||||||||||||
| 694 | 53961 | my $tab_width = $self->tab_width; | |||||||||||||||||||||||||||||||||||||||||
| 695 | 53961 | my ($length, $vis_inc); | |||||||||||||||||||||||||||||||||||||||||
| 696 | |||||||||||||||||||||||||||||||||||||||||||
| 697 | 53961 | ▶ | return length $content if $content !~ /\t/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 698 | |||||||||||||||||||||||||||||||||||||||||||
| 699 | # Split the content in tab and non-tab parts and calculate the | ||||||||||||||||||||||||||||||||||||||||||
| 700 | # "visual increase" of each part. | ||||||||||||||||||||||||||||||||||||||||||
| 701 | 4319 | for my $part ( split(/(\t)/, $content) ) { | |||||||||||||||||||||||||||||||||||||||||
| 702 | 15487 | ▶ | if ($part eq "\t") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 703 | 7716 | $vis_inc = $tab_width - ($pos-1) % $tab_width; | |||||||||||||||||||||||||||||||||||||||||
| 704 | } | ||||||||||||||||||||||||||||||||||||||||||
| 705 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 706 | 7771 | $vis_inc = length $part; | |||||||||||||||||||||||||||||||||||||||||
| 707 | } | ||||||||||||||||||||||||||||||||||||||||||
| 708 | 15487 | $length += $vis_inc; | |||||||||||||||||||||||||||||||||||||||||
| 709 | 15487 | $pos += $vis_inc; | |||||||||||||||||||||||||||||||||||||||||
| 710 | } | ||||||||||||||||||||||||||||||||||||||||||
| 711 | |||||||||||||||||||||||||||||||||||||||||||
| 712 | 4319 | $length; | |||||||||||||||||||||||||||||||||||||||||
| 713 | } | ||||||||||||||||||||||||||||||||||||||||||
| 714 | |||||||||||||||||||||||||||||||||||||||||||
| 715 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 716 | |||||||||||||||||||||||||||||||||||||||||||
| 717 | =head2 flush_locations | ||||||||||||||||||||||||||||||||||||||||||
| 718 | |||||||||||||||||||||||||||||||||||||||||||
| 719 | When no longer needed, the C<flush_locations> method clears all location data | ||||||||||||||||||||||||||||||||||||||||||
| 720 | from the tokens. | ||||||||||||||||||||||||||||||||||||||||||
| 721 | |||||||||||||||||||||||||||||||||||||||||||
| 722 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 723 | |||||||||||||||||||||||||||||||||||||||||||
| 724 | sub flush_locations { | ||||||||||||||||||||||||||||||||||||||||||
| 725 | 1 | ▶ | shift->_flush_locations(@_); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
flush_locations: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 726 | } | ||||||||||||||||||||||||||||||||||||||||||
| 727 | |||||||||||||||||||||||||||||||||||||||||||
| 728 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 729 | |||||||||||||||||||||||||||||||||||||||||||
| 730 | =head2 normalized | ||||||||||||||||||||||||||||||||||||||||||
| 731 | |||||||||||||||||||||||||||||||||||||||||||
| 732 | The C<normalized> method is used to generate a "Layer 1" | ||||||||||||||||||||||||||||||||||||||||||
| 733 | L<PPI::Document::Normalized> object for the current Document. | ||||||||||||||||||||||||||||||||||||||||||
| 734 | |||||||||||||||||||||||||||||||||||||||||||
| 735 | A "normalized" Perl Document is an arbitrary structure that removes any | ||||||||||||||||||||||||||||||||||||||||||
| 736 | irrelevant parts of the document and refactors out variations in style, | ||||||||||||||||||||||||||||||||||||||||||
| 737 | to attempt to approach something that is closer to the "true meaning" | ||||||||||||||||||||||||||||||||||||||||||
| 738 | of the Document. | ||||||||||||||||||||||||||||||||||||||||||
| 739 | |||||||||||||||||||||||||||||||||||||||||||
| 740 | See L<PPI::Normal> for more information on document normalization and | ||||||||||||||||||||||||||||||||||||||||||
| 741 | the tasks for which it is useful. | ||||||||||||||||||||||||||||||||||||||||||
| 742 | |||||||||||||||||||||||||||||||||||||||||||
| 743 | Returns a L<PPI::Document::Normalized> object, or C<undef> on error. | ||||||||||||||||||||||||||||||||||||||||||
| 744 | |||||||||||||||||||||||||||||||||||||||||||
| 745 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 746 | |||||||||||||||||||||||||||||||||||||||||||
| 747 | sub normalized { | ||||||||||||||||||||||||||||||||||||||||||
| 748 | # The normalization process will utterly destroy and mangle | ||||||||||||||||||||||||||||||||||||||||||
| 749 | # anything passed to it, so we are going to only give it a | ||||||||||||||||||||||||||||||||||||||||||
| 750 | # clone of ourselves. | ||||||||||||||||||||||||||||||||||||||||||
| 751 | 4 | ▶ | PPI::Normal->process( $_[0]->clone ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
normalized: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 752 | } | ||||||||||||||||||||||||||||||||||||||||||
| 753 | |||||||||||||||||||||||||||||||||||||||||||
| 754 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 755 | |||||||||||||||||||||||||||||||||||||||||||
| 756 | =head1 complete | ||||||||||||||||||||||||||||||||||||||||||
| 757 | |||||||||||||||||||||||||||||||||||||||||||
| 758 | The C<complete> method is used to determine if a document is cleanly | ||||||||||||||||||||||||||||||||||||||||||
| 759 | structured, all braces are closed, the final statement is | ||||||||||||||||||||||||||||||||||||||||||
| 760 | fully terminated and all heredocs are fully entered. | ||||||||||||||||||||||||||||||||||||||||||
| 761 | |||||||||||||||||||||||||||||||||||||||||||
| 762 | Returns true if the document is complete or false if not. | ||||||||||||||||||||||||||||||||||||||||||
| 763 | |||||||||||||||||||||||||||||||||||||||||||
| 764 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 765 | |||||||||||||||||||||||||||||||||||||||||||
| 766 | sub complete { | ||||||||||||||||||||||||||||||||||||||||||
| 767 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
complete: called CC=5 SCAR=16.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 768 | |||||||||||||||||||||||||||||||||||||||||||
| 769 | # Every structure has to be complete | ||||||||||||||||||||||||||||||||||||||||||
| 770 | $self->find_any( sub { | ||||||||||||||||||||||||||||||||||||||||||
| 771 | 15 | ▶ | $_[1]->isa('PPI::Structure') | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 772 | and | ||||||||||||||||||||||||||||||||||||||||||
| 773 | ! $_[1]->complete | ||||||||||||||||||||||||||||||||||||||||||
| 774 | } ) | ||||||||||||||||||||||||||||||||||||||||||
| 775 | 2 | ▶ | and return ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 776 | |||||||||||||||||||||||||||||||||||||||||||
| 777 | # Strip anything that isn't a statement off the end | ||||||||||||||||||||||||||||||||||||||||||
| 778 | 2 | my @child = $self->children; | |||||||||||||||||||||||||||||||||||||||||
| 779 | 2 | ▶ | while ( @child and not $child[-1]->isa('PPI::Statement') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@child and not $child[-1]->isa("PPI::Statement")
MC/DC50%1/2
@child and not $child[-1]->isa("PPI::Statement")
@child
not $child[-1]->isa("PPI::Statement")
Truth table66%2/3
A and not(B)
A= @childB= not $child[-1]->isa("PPI::Statement")
| |||||||||||||||||||||||||||||||||||||||||||
| 780 | 2 | pop @child; | |||||||||||||||||||||||||||||||||||||||||
| 781 | } | ||||||||||||||||||||||||||||||||||||||||||
| 782 | |||||||||||||||||||||||||||||||||||||||||||
| 783 | # We must have at least one statement | ||||||||||||||||||||||||||||||||||||||||||
| 784 | 2 | ▶ | return '' unless @child; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 785 | |||||||||||||||||||||||||||||||||||||||||||
| 786 | # Check the completeness of the last statement | ||||||||||||||||||||||||||||||||||||||||||
| 787 | 2 | return $child[-1]->_complete; | |||||||||||||||||||||||||||||||||||||||||
| 788 | } | ||||||||||||||||||||||||||||||||||||||||||
| 789 | |||||||||||||||||||||||||||||||||||||||||||
| 790 | |||||||||||||||||||||||||||||||||||||||||||
| 791 | |||||||||||||||||||||||||||||||||||||||||||
| 792 | |||||||||||||||||||||||||||||||||||||||||||
| 793 | |||||||||||||||||||||||||||||||||||||||||||
| 794 | ##################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 795 | # PPI::Node Methods | ||||||||||||||||||||||||||||||||||||||||||
| 796 | |||||||||||||||||||||||||||||||||||||||||||
| 797 | # We are a scope boundary | ||||||||||||||||||||||||||||||||||||||||||
| 798 | ### XS -> PPI/XS.xs:_PPI_Document__scope 0.903+ | ||||||||||||||||||||||||||||||||||||||||||
| 799 | sub scope() { 1 } | ||||||||||||||||||||||||||||||||||||||||||
| 800 | |||||||||||||||||||||||||||||||||||||||||||
| 801 | |||||||||||||||||||||||||||||||||||||||||||
| 802 | |||||||||||||||||||||||||||||||||||||||||||
| 803 | |||||||||||||||||||||||||||||||||||||||||||
| 804 | |||||||||||||||||||||||||||||||||||||||||||
| 805 | ##################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 806 | # PPI::Element Methods | ||||||||||||||||||||||||||||||||||||||||||
| 807 | |||||||||||||||||||||||||||||||||||||||||||
| 808 | sub insert_before { | ||||||||||||||||||||||||||||||||||||||||||
| 809 | 0 | ▶ | return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
insert_before: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 810 | # die "Cannot insert_before a PPI::Document"; | ||||||||||||||||||||||||||||||||||||||||||
| 811 | } | ||||||||||||||||||||||||||||||||||||||||||
| 812 | |||||||||||||||||||||||||||||||||||||||||||
| 813 | sub insert_after { | ||||||||||||||||||||||||||||||||||||||||||
| 814 | 0 | ▶ | return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
insert_after: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 815 | # die "Cannot insert_after a PPI::Document"; | ||||||||||||||||||||||||||||||||||||||||||
| 816 | } | ||||||||||||||||||||||||||||||||||||||||||
| 817 | |||||||||||||||||||||||||||||||||||||||||||
| 818 | sub replace { | ||||||||||||||||||||||||||||||||||||||||||
| 819 | 0 | ▶ | return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
replace: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 820 | # die "Cannot replace a PPI::Document"; | ||||||||||||||||||||||||||||||||||||||||||
| 821 | } | ||||||||||||||||||||||||||||||||||||||||||
| 822 | |||||||||||||||||||||||||||||||||||||||||||
| 823 | |||||||||||||||||||||||||||||||||||||||||||
| 824 | |||||||||||||||||||||||||||||||||||||||||||
| 825 | |||||||||||||||||||||||||||||||||||||||||||
| 826 | |||||||||||||||||||||||||||||||||||||||||||
| 827 | ##################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 828 | # Error Handling | ||||||||||||||||||||||||||||||||||||||||||
| 829 | |||||||||||||||||||||||||||||||||||||||||||
| 830 | # Set the error message | ||||||||||||||||||||||||||||||||||||||||||
| 831 | sub _error { | ||||||||||||||||||||||||||||||||||||||||||
| 832 | 1 | ▶ | $errstr = $_[1]; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_error: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 833 | 1 | undef; | |||||||||||||||||||||||||||||||||||||||||
| 834 | } | ||||||||||||||||||||||||||||||||||||||||||
| 835 | |||||||||||||||||||||||||||||||||||||||||||
| 836 | # Clear the error message. | ||||||||||||||||||||||||||||||||||||||||||
| 837 | # Returns the object as a convenience. | ||||||||||||||||||||||||||||||||||||||||||
| 838 | sub _clear { | ||||||||||||||||||||||||||||||||||||||||||
| 839 | 0 | ▶ | $errstr = ''; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_clear: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 840 | 0 | $_[0]; | |||||||||||||||||||||||||||||||||||||||||
| 841 | } | ||||||||||||||||||||||||||||||||||||||||||
| 842 | |||||||||||||||||||||||||||||||||||||||||||
| 843 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 844 | |||||||||||||||||||||||||||||||||||||||||||
| 845 | =head2 errstr | ||||||||||||||||||||||||||||||||||||||||||
| 846 | |||||||||||||||||||||||||||||||||||||||||||
| 847 | For error that occur when loading and saving documents, you can use | ||||||||||||||||||||||||||||||||||||||||||
| 848 | C<errstr>, as either a static or object method, to access the error message. | ||||||||||||||||||||||||||||||||||||||||||
| 849 | |||||||||||||||||||||||||||||||||||||||||||
| 850 | If a Document loads or saves without error, C<errstr> will return false. | ||||||||||||||||||||||||||||||||||||||||||
| 851 | |||||||||||||||||||||||||||||||||||||||||||
| 852 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 853 | |||||||||||||||||||||||||||||||||||||||||||
| 854 | sub errstr { | ||||||||||||||||||||||||||||||||||||||||||
| 855 | 12 | ▶ | $errstr; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
errstr: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 856 | } | ||||||||||||||||||||||||||||||||||||||||||
| 857 | |||||||||||||||||||||||||||||||||||||||||||
| 858 | |||||||||||||||||||||||||||||||||||||||||||
| 859 | |||||||||||||||||||||||||||||||||||||||||||
| 860 | |||||||||||||||||||||||||||||||||||||||||||
| 861 | |||||||||||||||||||||||||||||||||||||||||||
| 862 | ##################################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 863 | # Native Storable Support | ||||||||||||||||||||||||||||||||||||||||||
| 864 | |||||||||||||||||||||||||||||||||||||||||||
| 865 | sub STORABLE_freeze { | ||||||||||||||||||||||||||||||||||||||||||
| 866 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
STORABLE_freeze: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 867 | 4 | my $class = ref $self; | |||||||||||||||||||||||||||||||||||||||||
| 868 | 4 | my %hash = %$self; | |||||||||||||||||||||||||||||||||||||||||
| 869 | 4 | return ($class, \%hash); | |||||||||||||||||||||||||||||||||||||||||
| 870 | } | ||||||||||||||||||||||||||||||||||||||||||
| 871 | |||||||||||||||||||||||||||||||||||||||||||
| 872 | sub STORABLE_thaw { | ||||||||||||||||||||||||||||||||||||||||||
| 873 | 6 | ▶ | my ($self, undef, $class, $hash) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
STORABLE_thaw: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 874 | 6 | bless $self, $class; | |||||||||||||||||||||||||||||||||||||||||
| 875 | 6 | foreach ( keys %$hash ) { | |||||||||||||||||||||||||||||||||||||||||
| 876 | 18 | $self->{$_} = delete $hash->{$_}; | |||||||||||||||||||||||||||||||||||||||||
| 877 | } | ||||||||||||||||||||||||||||||||||||||||||
| 878 | 6 | $self->__link_children; | |||||||||||||||||||||||||||||||||||||||||
| 879 | } | ||||||||||||||||||||||||||||||||||||||||||
| 880 | |||||||||||||||||||||||||||||||||||||||||||
| 881 | 62 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 882 | |||||||||||||||||||||||||||||||||||||||||||
| 883 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 884 | |||||||||||||||||||||||||||||||||||||||||||
| 885 | =head1 TO DO | ||||||||||||||||||||||||||||||||||||||||||
| 886 | |||||||||||||||||||||||||||||||||||||||||||
| 887 | - May need to overload some methods to forcefully prevent Document | ||||||||||||||||||||||||||||||||||||||||||
| 888 | objects becoming children of another Node. | ||||||||||||||||||||||||||||||||||||||||||
| 889 | |||||||||||||||||||||||||||||||||||||||||||
| 890 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||
| 891 | |||||||||||||||||||||||||||||||||||||||||||
| 892 | See the L<support section|PPI/SUPPORT> in the main module. | ||||||||||||||||||||||||||||||||||||||||||
| 893 | |||||||||||||||||||||||||||||||||||||||||||
| 894 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 895 | |||||||||||||||||||||||||||||||||||||||||||
| 896 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||||||||||||||||||||||||||||||||||||||||||
| 897 | |||||||||||||||||||||||||||||||||||||||||||
| 898 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 899 | |||||||||||||||||||||||||||||||||||||||||||
| 900 | L<PPI>, L<http://ali.as/> | ||||||||||||||||||||||||||||||||||||||||||
| 901 | |||||||||||||||||||||||||||||||||||||||||||
| 902 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 903 | |||||||||||||||||||||||||||||||||||||||||||
| 904 | Copyright 2001 - 2011 Adam Kennedy. | ||||||||||||||||||||||||||||||||||||||||||
| 905 | |||||||||||||||||||||||||||||||||||||||||||
| 906 | This program is free software; you can redistribute | ||||||||||||||||||||||||||||||||||||||||||
| 907 | it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 908 | |||||||||||||||||||||||||||||||||||||||||||
| 909 | The full text of the license can be found in the | ||||||||||||||||||||||||||||||||||||||||||
| 910 | LICENSE file included with this module. | ||||||||||||||||||||||||||||||||||||||||||
| 911 | |||||||||||||||||||||||||||||||||||||||||||
| 912 | =cut | ||||||||||||||||||||||||||||||||||||||||||