File Coverage

blib/lib/PPI/Document.pm
Criterion Covered Total %
statement 231 252 91.6
branch 81 116 69.8
condition 17 26 65.3
subroutine 41 45 91.1
pod 16 20 80.0
total 386 459 84.1


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