blib/lib/File/Parser/Role.pm

filter: Statementstmt 98.5% 66 / 67 Branchbran 79.1% 19 / 24 Conditioncond 81.8% 9 / 11 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 1 totaltotal 91.0% 112 / 123 SCAR 28.4
CC
17
Cov
92%
__ANON__
10.2
_build_fh
8.2
BEGIN
1.0
CRAP 17.1
↑ Summary Build »
1 package File::Parser::Role;
2  
3 6 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use IO::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 6 ▶use version; our $VERSION = qv('0.2.1');
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use MooX::Aliases;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # File things
17 6 has file => ( is => "ro", alias => [qw/path filepath/] );
18 6 has size => ( is => "ro" );
19 6 has filename => ( is => "ro" );
20 6 has encoding => ( is => "ro" );
21 6 has fh => ( is => "lazy" );
22  
23 6 requires "parse";
24  
25 sub _build_fh {
26  
27 38 ▶ my $self = shift;
Subroutine _build_fh: called CC=8 SCAR=21.0
28  
29 ## If stringified input is a readable file, treat it like that
30 38 ▶ if ( -r "${\ $self->file }" ) {
Branch: 3 branches
BranchTrueFalseTotal
if (-r "${\$self->file;}") { } 15 23 38
elsif (ref $self->file eq 'SCALAR') { } 11 12 23
elsif (ref $self->file) { } 12 0 12
31  
32 15 my $fh = IO::File->new( $self->file, "r" );
33  
34 ## set it to the (possibly) specified encoding
35 15 ▶ if ( defined $self->encoding ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->encoding) 2 13 15
36 2 ▶ binmode $fh, sprintf(":encoding(%s)", $self->encoding) or confess $!;
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
binmode $fh, sprintf(":encoding(%s)", $self->encoding) or confess($!) 0 2 2
37 }
38 15 return $fh;
39  
40 }
41  
42 ## A scalar reference is assumed to be content to be parsed
43 elsif ( ref $self->file eq "SCALAR" ) {
44 11 return IO::String->new( $self->file );
45 }
46  
47 ## If it's any kind of object, assume it can be <read> from
48 elsif ( ref $self->file ) {
49  
50 ## assume its something that can be read from as a file handle
51 ## set encoding and use it
52 12 ▶ if ( defined $self->encoding ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->encoding) 2 10 12
53 2 ▶ binmode $self->file, sprintf(":encoding(%s)", $self->encoding) or confess $!;
Branch: 1 branch
BranchTrueFalseTotal
binmode $self->file, sprintf(":encoding(%s)", $self->encoding) or confess($!) 0 2 2
54 }
55 12 return $self->file;
56  
57 }
58  
59 ## can't grok it
60 else {
61 0 confess "Cannot work with input file - its neither a readable path nor a reference";
62 }
63  
64 }
65  
66 around BUILDARGS => sub {
67  
68 39 ▶ my ($orig, $class) = (shift, shift);
Subroutine __ANON__: called CC=10 SCAR=23.2
69  
70 39 my @args = @_;
71  
72 39 ▶ if ( @args == 1 and (ref( $args[0])||'') ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1 and (ref $args[0] || "") ne "HASH") 14 25 39
Condition80%4/5
@args == 1 and (ref $args[0] || "") ne "HASH"
!ll&&!rl&&r
02514
ref $args[0] || ''
l!l
354
MC/DC50%1/2
@args == 1 and (ref $args[0] || "") ne "HASH"
@args == 1 ref $args[0]
Truth table66%2/3
A and B
A= @args == 1B= ref $args[0]
ABresult
0X0
100
111
73 14 @args = ({ file => $args[0] });
74 }
75  
76 39 ▶ if ( not exists $args[0]->{file} and defined $args[0]->{filename} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $args[0]{"file"} and defined $args[0]{"filename"}) 3 36 39
Condition100%3/3
not exists $args[0]{"file"} and defined $args[0]{"filename"}
!ll&&!rl&&r
3333
MC/DC100%2/2
not exists $args[0]{"file"} and defined $args[0]{"filename"}
not exists $args[0]{"file"} defined $args[0]{"filename"}
Truth table100%3/3
not(A) and B
A= not exists $args[0]{"file"}B= defined $args[0]{"filename"}
ABresult
0X0
100
111
77 ## filename gets deleted for now and only re-inserted later on
78 ## if proven to be a valid filename
79 3 $args[0]->{file} = delete $args[0]->{filename};
80 }
81  
82 ## capture the aliases this way
83 39 my $obj = $class->$orig(@args);
84 39 my $f = $obj->{file};
85  
86 ## test if it seems to be a path to a file
87 39 ▶ if ( defined $f and -e "$f" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $f and -e "$f") 16 23 39
Condition66%2/3
defined $f and -e "$f"
!ll&&!rl&&r
02316
MC/DC50%1/2
defined $f and -e "$f"
defined $f -e "$f"
Truth table66%2/3
A and B
A= defined $fB= -e "$f"
ABresult
0X0
100
111
88  
89 ## size (most likely) and filename can now be set
90  
91 ## only sets/overrides size if it isn't already set
92 16 ▶ $obj->{size} = -s "$f" unless exists $obj->{size};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $obj->{"size"} 16 0 16
93  
94 ## set filename if not already set
95 16 ▶ $obj->{filename} = "$f" unless defined $obj->{filename};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj->{"filename"} 16 0 16
96  
97 }
98  
99 39 return $obj;
100  
101 6 };
102  
103 sub BUILD {
104 38 ▶ my $self = shift;
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
105 38 $self->parse;
106 };
107  
108 6 1; # Magic true value required at end of module
109 __END__
110  
111 =encoding utf8
112  
113 =head1 NAME
114  
115 File::Parser::Role - Read and prepare parsing of file (or glob) data
116 from some source
117  
118 =head1 VERSION
119  
120 This document describes File::Parser::Role version 0.2.1 This is a
121 Moo::Role for reading (and then parsing) single data files. It makes
122 the constructor support 3 kinds of file sources:
123  
124 =over
125  
126 =item a path to a readable file
127  
128 =item a file handle or anything that can be read like one
129  
130 =item a scalar references to content
131  
132 =back
133  
134 It also provides a method "fh" that gives an at least readable file
135 handle to the contents of the file.
136  
137 =head1 SYNOPSIS
138  
139 package MyClassThatDoesStuffToAFile;
140  
141 sub parse {
142 my $self = shift;
143  
144 # ... do stuff, $self->fh available
145 }
146  
147 with "File::Parser::Role";
148  
149 ## ... and in some nearby code:
150  
151 my $obj = MyClassThatDoesStuffToAFile->new("some_file.txt");
152 # or #
153 my $obj = MyClassThatDoesStuffToAFile->new(file => "some_file.txt");
154 ## optinally:
155  
156 my $obj = MyClassThatDoesStuffToAFile->new( file => "some_file.txt", encoding => "utf8" );
157 ## encoding can be anything that binmode's encoding() can understand.
158  
159 print $obj->filename; # "some_file.txt"
160 print $obj->size; # size of some_file.txt
161  
162 ## - OR -
163  
164 my $fh = IO::File->new( "< some_file.txt" );
165 ## you are now responsible for encoding on this handle!
166  
167 my $obj = MyClassThatDoesStuffToAFile->new( file => $fh );
168  
169 ## no filename nor file size available
170  
171 ## - OR -
172  
173 my $file_content = read_file( "some_file.txt" );
174 my $obj = MyClassThatDoesStuffToAFile->new( file => \$file_content );
175  
176 ## you are also responsible for encoding on this data
177 ## no file name nor file size available
178  
179 =head1 DESCRIPTION
180  
181 This role provides all the bare necessities, and then some, that you
182 expect when you want to parse or otherwise handle a chunk of content
183 typically provided as a file.
184  
185 It is motivated by and ideal for objects that parse files.
186  
187 =head1 INTERFACE
188  
189 =head2 new
190  
191 The constructor is meant to be all expected kinds of flexible:
192  
193 =over
194  
195 =item * new("file")
196  
197 =item * new($fh)
198  
199 =item * new( file => "file", encoding => "utf8" ); # also works with: C<new({ ... })>
200  
201 =item * new( \"some content" )
202  
203 =back
204  
205 Construction tests the argument and if it's a path to a file reachable
206 on the local file system records its C<filename> and C<size> in those
207 two attributes.
208  
209 When it checks if the argument if a local filename, it checks "$file",
210 allowing objects that stringify to paths to work correctly. This
211 applies among others to Path::Tiny.
212  
213 If a reference to something is passed (or an object), it is assumed to
214 be something that can be read with <> and passed for a handle.
215  
216 =head2 fh
217  
218 Returns ro handle (IO::File for files, IO::String for content) to the
219 contents of the input, be it a file or a sclar reference or an already
220 opened file handle
221  
222 If the input argument is assumed to be a readable handle to content,
223 it is passed straight through with this method.
224  
225 =head2 parse
226  
227 A required method that you must write! It is run in BUILD
228  
229 =head1 DIAGNOSTICS
230  
231 =over
232  
233 =item C<< Cannot work with input file >>
234  
235 The file argument is neither an existing file, an object nor a
236 reference to content
237  
238 =back
239  
240 =head1 DEPENDENCIES
241  
242 =over
243  
244 =item * L<Moo>
245  
246 =item * L<Moo::Role>
247  
248 =item * L<IO::String>
249  
250 =item * L<Pod::Coverage::Moose>
251  
252 =item * L<Test::Most>
253  
254 =item * L<Test::Perl::Critic>
255  
256 =item * L<Test::Pod>
257  
258 =item * L<Test::Pod::Coverage>
259  
260 =item * L<Test::Output>
261  
262 =item * L<Test::Warnings>
263  
264 =item * L<MooX::Aliases>
265  
266 =back
267  
268 =head1 INCOMPATIBILITIES
269  
270 None reported.
271  
272 =head1 BUGS AND LIMITATIONS
273  
274 No bugs have been reported.
275  
276 Please report any bugs or feature requests to
277 C<bug-file-parser-role@rt.cpan.org>, or through the web interface at
278 L<http://rt.cpan.org>.
279  
280 =head1 AUTHOR
281  
282 Torbjørn Lindahl C<< <torbjorn.lindahl@gmail.com> >>
283  
284 =head1 LICENCE AND COPYRIGHT
285  
286 Copyright (c) 2012, Torbjørn Lindahl C<<
287 <torbjorn.lindahl@gmail.com> >>. All rights reserved.
288  
289 This module is free software; you can redistribute it and/or
290 modify it under the same terms as Perl itself. See L<perlartistic>.
291  
292  
293 =head1 DISCLAIMER OF WARRANTY
294  
295 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
296 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
297 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
298 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
299 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
300 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
301 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
302 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
303 NECESSARY SERVICING, REPAIR, OR CORRECTION.
304  
305 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
306 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
307 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
308 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
309 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
310 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
311 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
312 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
313 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
314 SUCH DAMAGES.
↑ Summary Build »