All/IO/File.pm

filter: Statementstmt 92.3% 72 / 78 Branchbran 68.7% 11 / 16 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 88.8% 16 / 18 Podpod 0.0% 0 / 10 totaltotal 79.5% 101 / 127 CC 10 SCAR 9.0
CC
10
Cov
88%
count
6.0
open
4.0
getrecord_array
4.0
CRAP 2.5
« All/Format/XML.pm ↑ Summary All/IO/Base.pm »
1 package Data::All::IO::File;
2  
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Data::All::IO::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 1 our $VERSION = 0.11;
14  
15 1 internal 'IO';
16 1 internal 'fh';
17  
18 1 attribute 'format';
19 1 attribute 'fields';
20 1 attribute 'ioconf';
21 1 attribute 'path';
22  
23 1 attribute 'is_open' => 0;
24  
25 1 internal 'FORMAT';
26 1 internal 'curpos' => -1;
27 1 internal 'added_fields' => {};
28  
29  
30 sub create_path()
31 10 ▶{
Subroutine create_path: called CC=1 SCAR=0.0
Pod undocumented
32 10 my $self = shift;
33 10 return join '', @{ $self->path };
34 }
35  
36 sub open($)
37 6 ▶{
Subroutine open: called CC=4 SCAR=13.9
Pod undocumented
38 6 my $self = shift;
39 6 my $path = $self->create_path();
40
41 6 ▶ unless ($self->is_open())
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_open) 4 2 6
42 {
43 #warn " -> Opening $path for ", $self->ioconf()->{'perm'};
44 #warn " -> path:", join ', ', @{ $self->path() };
45 #warn " -> format:", $self->format()->{'type'};
46 #warn " -> io:", $self->ioconf->{'type'};
47
48 die("The file: $path does not exist")
49 4 ▶ if (($self->ioconf()->{'perm'} eq 'r') && !(-f $path));
Branch: 1 branch
BranchTrueFalseTotal
if $self->ioconf->{"perm"} eq "r" and not -f $path 0 4 4
Condition66%2/3
$self->ioconf->{"perm"} eq "r" and not -f $path
!ll&&!rl&&r
220
MC/DC0%0/2
$self->ioconf->{"perm"} eq "r" and not -f $path
$self->ioconf->{"perm"} eq "r" not -f $path
Truth table66%2/3
A and not(B)
A= $self->ioconf->{"perm"} eq "r"B= not -f $path
ABresult
0X0
100
111
50
51 # We create out own filehandle for better read/write control
52 4 my $fh = FileHandle->new($self->create_path(), $self->ioconf()->{'perm'});
53
54 4 $self->__IO( $fh );
55 4 $self->__fh( $fh );
56
57 4 $self->is_open(1);
58
59 4 $self->_extract_fields(); # Initialize field names
60 }
61
62 6 return $self->is_open();
63 }
64  
65 sub close()
66 2 ▶{
Subroutine close: called CC=1 SCAR=0.0
Pod undocumented
67 2 my $self = shift;
68
69 2 $self->__fh()->close();
70
71 2 $self->__IO()->close();
72 2 $self->is_open(0);
73 }
74  
75 sub nextrecord()
76 12 ▶{
Subroutine nextrecord: called CC=2 SCAR=6.9
Pod undocumented
77 12 my $self = shift;
78 12 my $r;
79
80 # TODO: Write an actual solution for converting from
81 # one line terminator to another.
82  
83 # Incrememnt cursor and remove trailing line
84 12 ▶ if ($r = $self->__fh()->getline())
Branch: 1 branch
BranchTrueFalseTotal
if ($r = $self->__fh->getline) 8 4 12
85 {
86 8 $r =~ s/\r\n/\n/g; # NOTE: a quick hack to convert DOS to UNIX
87 8 chomp($r);
88 8 $self->_next();
89 }
90
91 12 return $r;
92 }
93  
94 sub hash_to_record()
95 3 ▶{
Subroutine hash_to_record: called CC=1 SCAR=0.0
Pod undocumented
96 3 my ($self, $hash) = @_;
97
98 # we do it like this to make sure the order is the same
99 3 return $self->array_to_record($self->hash_to_array($hash));
100 }
101  
102 sub array_to_record()
103 3 ▶{
Subroutine array_to_record: called CC=1 SCAR=0.0
Pod undocumented
104 3 my ($self, $array) = @_;
105 3 return $self->__FORMAT()->contract($array);
106 }
107  
108  
109  
110 sub getrecord_array()
111 # With original = include original record from file
112 12 ▶{
Subroutine getrecord_array: called CC=4 SCAR=13.9
Pod undocumented
113 12 my ($self, $with_original) = @_;
114 12 my $raw;
115
116 12 ▶ return undef unless ($raw = $self->nextrecord());
Branch: 1 branch
BranchTrueFalseTotal
unless $raw = $self->nextrecord 4 8 12
117
118 # We return the original record first b/c if we do it
119 # last and there are empty values at the end the order will be confused
120 8 ▶ my $rec_arrayref = ($with_original)
Branch: 1 branch
BranchTrueFalseTotal
$with_original ? : 0 8 8
121 ? [$raw, $self->__FORMAT()->expand($raw)]
122 : [$self->__FORMAT()->expand($raw)];
123
124 8 ▶ return !wantarray ? $rec_arrayref : @{ $rec_arrayref };
Branch: 1 branch
BranchTrueFalseTotal
!wantarray ? : 6 2 8
125 }
126
127 sub putfields()
128 0 ▶{
Subroutine putfields: not called CC=1 SCAR=6.9
Pod undocumented
129 0 my $self = shift;
130 0 $self->__IO()->print($self->array_to_record($self->fields));
131 }
132  
133 sub putrecord($)
134 3 ▶{
Subroutine putrecord: called CC=1 SCAR=0.0
Pod undocumented
135 3 my $self = shift;
136 3 my $record = shift;
137
138 3 $self->__IO()->print($self->hash_to_record($record));
139
140 3 return 1;
141 }
142  
143  
144 sub _extract_fields()
145 4 ▶{
Subroutine _extract_fields: called CC=2 SCAR=7.1
146 4 my $self = shift;
147 4 ▶ return if ($self->fields());
Branch: 1 branch
BranchTrueFalseTotal
if $self->fields 0 4 4
148 4 $self->fields([$self->getrecord_array(0)]);
149 }
150  
151 sub count()
152 0 ▶{
Subroutine count: not called CC=2 SCAR=17.9
Pod undocumented
153 0 my $self = shift;
154 0 my $count;
155
156 # From the Perl Cookbook. It doesn't actually replace every
157 # new line with a new new line -- it's a legacy feature.
158 0 ▶ $count += tr/\n/\n/ while sysread($self->__fh(), $_, 2 ** 20);
Branch: 1 branch
BranchTrueFalseTotal
sysread $self->__fh, $_, 1048576 and do { $count += tr/\n// } 0 0 0
159
160 0 return $count;
161 }
162 8 ▶sub _next() { $_[0]->__curpos( $_[0]->__curpos() + 1) }
Subroutine _next: called CC=1 SCAR=0.0
163  
164  
165 1 1;
« All/Format/XML.pm ↑ Summary All/IO/Base.pm »