PPI/Document/File.pm

filter: Statementstmt 79.4% 27 / 34 Branchbran 37.5% 3 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 88.8% 8 / 9 Podpod 100% 3 / 3 totaltotal 75.9% 41 / 54 CC 5 SCAR 10.4
CC
5
Cov
71%
save
6.0
new
4.5
BEGIN
1.0
CRAP 2.8
« PPI/Token/Number/Version.pm ↑ Summary PPI/Token/_QuoteEngine/Simple.pm »
1 package PPI::Document::File;
2  
3 =pod
4  
5 =head1 NAME
6  
7 PPI::Document::File - A Perl Document located in a specific file
8  
9 =head1 DESCRIPTION
10  
11 B<WARNING: This class is experimental, and may change without notice>
12  
13 B<PPI::Document::File> provides a L<PPI::Document> subclass that represents
14 a Perl document stored in a specific named file.
15  
16 =head1 METHODS
17  
18 =cut
19  
20 62 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 62 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 62 ▶use Params::Util qw{_STRING _INSTANCE};
Subroutine BEGIN: called CC=1 SCAR=0.0
23 62 ▶use PPI::Document ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 62 ▶use vars qw{$VERSION @ISA};
Subroutine BEGIN: called CC=1 SCAR=0.0
26 BEGIN {
27 62 ▶ $VERSION = '1.236';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 62 @ISA = 'PPI::Document';
29 }
30  
31  
32  
33  
34  
35 #####################################################################
36 # Constructor and Accessors
37  
38 =pod
39  
40 =head2 new
41  
42 my $file = PPI::Document::File->new( 'Module.pm' );
43  
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.
47  
48 Returns a new PPI::Document::File object, or C<undef> on error.
49  
50 =cut
51  
52 sub new {
53 1 ▶ my $class = shift;
Subroutine new: called CC=4 SCAR=15.0
Pod documented
54 1 my $filename = _STRING(shift);
55 1 ▶ unless ( defined $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $filename) 0 1 1
56 # Perl::Critic got a complaint about not handling a file
57 # named "0".
58 0 return $class->_error("Did not provide a file name to load");
59 }
60  
61 # Load the Document
62 1 ▶ my $self = $class->SUPER::new( $filename, @_ ) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $self = $class->SUPER::new($filename, @_) or return undef 0 1 1
63  
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 1 ▶ if ( _INSTANCE($self, 'PPI::Document') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_INSTANCE($self, 'PPI::Document')) { } 1 0 1
68 1 bless $self, 'PPI::Document::File';
69 } else {
70 0 die "PPI::Document::File SUPER call returned an object of the wrong type";
71 }
72  
73 # Save the filename
74 1 $self->{filename} = $filename;
75  
76 1 $self;
77 }
78  
79 =head2 filename
80  
81 The C<filename> accessor returns the name of the file in which the document
82 is stored.
83  
84 =cut
85  
86 sub filename {
87 1 ▶ $_[0]->{filename};
Subroutine filename: called CC=1 SCAR=0.0
Pod documented
88 }
89  
90 =pod
91  
92 =head2 save
93  
94 # Save to the file we were loaded from
95 $file->save;
96
97 # Save a copy to somewhere else
98 $file->save( 'Module2.pm' );
99  
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.
102  
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.
105  
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.
109  
110 =cut
111  
112 sub save {
113 0 ▶ my $self = shift;
Subroutine save: not called CC=2 SCAR=17.9
Pod documented
114  
115 # Save to where?
116 0 my $filename = shift;
117 0 ▶ unless ( defined $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $filename) 0 0 0
118 0 $filename = $self->filename;
119 }
120  
121 # Hand off to main save method
122 0 $self->SUPER::save( $filename, @_ );
123 }
124  
125 62 1;
126  
127 =pod
128  
129 =head1 TO DO
130  
131 - May need to overload some methods to forcefully prevent Document
132 objects becoming children of another Node.
133  
134 =head1 SUPPORT
135  
136 See the L<support section|PPI/SUPPORT> in the main module.
137  
138 =head1 AUTHOR
139  
140 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
141  
142 =head1 COPYRIGHT
143  
144 Copyright 2001 - 2011 Adam Kennedy.
145  
146 This program is free software; you can redistribute
147 it and/or modify it under the same terms as Perl itself.
148  
149 The full text of the license can be found in the
150 LICENSE file included with this module.
151  
152 =cut
« PPI/Token/Number/Version.pm ↑ Summary PPI/Token/_QuoteEngine/Simple.pm »