1 package PPI::Document::File;
7 PPI::Document::File - A Perl Document located in a specific file
11 B<WARNING: This class is experimental, and may change without notice>
13 B<PPI::Document::File> provides a L<PPI::Document> subclass that represents
14 a Perl document stored in a specific named file.
22 use Params::Util qw{_STRING _INSTANCE};
25 use vars qw{$VERSION @ISA};
28 @ISA = 'PPI::Document';
35 #####################################################################
36 # Constructor and Accessors
42 my $file = PPI::Document::File->new( 'Module.pm' );
44 The C<new> constructor works the same as for the regular one, except
45 that the only params allowed is a file name. You cannot create an
46 "anonymous" PPI::Document::File object, not can you create an empty one.
48 Returns a new PPI::Document::File object, or C<undef> on error.
54 my $filename = _STRING(shift);
55 unless ( defined $filename ) {
56 # Perl::Critic got a complaint about not handling a file
58 return $class->_error("Did not provide a file name to load");
62 my $self = $class->SUPER::new( $filename, @_ ) or return undef;
64 # Unlike a normal inheritance situation, due to our need to stay
65 # compatible with caching magic, this actually returns a regular
66 # anonymous document. We need to rebless if
67 if ( _INSTANCE($self, 'PPI::Document') ) {
68 bless $self, 'PPI::Document::File';
70 die "PPI::Document::File SUPER call returned an object of the wrong type";
74 $self->{filename} = $filename;
81 The C<filename> accessor returns the name of the file in which the document
94 # Save to the file we were loaded from
97 # Save a copy to somewhere else
98 $file->save( 'Module2.pm' );
100 The C<save> method works similarly to the one in the parent L<PPI::Document>
101 class, saving a copy of the document to a file.
103 The difference with this subclass is that if C<save> is not passed any
104 filename, it will save it back to the file it was loaded from.
106 Note: When saving to a different file, it is considered to be saving a
107 B<copy> and so the value returned by the C<filename> accessor will stay
108 the same, and not change to the new filename.
116 my $filename = shift;
117 unless ( defined $filename ) {
118 $filename = $self->filename;
121 # Hand off to main save method
122 $self->SUPER::save( $filename, @_ );
131 - May need to overload some methods to forcefully prevent Document
132 objects becoming children of another Node.
136 See the L<support section|PPI/SUPPORT> in the main module.
140 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
144 Copyright 2001 - 2009 Adam Kennedy.
146 This program is free software; you can redistribute
147 it and/or modify it under the same terms as Perl itself.
149 The full text of the license can be found in the
150 LICENSE file included with this module.