AnyData.pm

filter: Statementstmt 57.1% 281 / 492 Branchbran 41.7% 112 / 268 Conditioncond 38.5% 57 / 148 MC/DCmcdc 14.4% 13 / 90 Subroutinesub 69.3% 34 / 49 Podpod 18.6% 8 / 43 totaltotal 46.3% 505 / 1090 CC 196 SCAR 42.9
CC
196
Cov
50%
is_matched
368.3
fetch_row
110.0
fetch_rowNEW
110.0
CRAP 73.2
« AnyData/Storage/RAM.pm ↑ Summary AnyData/Storage/File.pm »
1 ##################################################################
2 package AnyData;
3 ###################################################################
4 #
5 # This module is copyright (c), 2000 by Jeff Zucker
6 # All rights reserved.
7 #
8 ###################################################################
9 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 require Exporter;
12 6 ▶use AnyData::Storage::TiedHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use vars qw( @ISA @EXPORT $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 @ISA = qw(Exporter);
15 6 @EXPORT = qw( adConvert adTie adRows adColumn adExport adDump adNames adFormats);
16 #@EXPORT = qw( ad_fields adTable adErr adArray);
17  
18 6 $VERSION = '0.12';
19  
20 12 ▶sub new {
Subroutine new: called CC=25 SCAR=38.2
Pod undocumented
21 12 my $class = shift;
22 12 my $format = shift;
23 12 ▶ my $flags = shift || {};
Condition50%1/2
shift() || {}
l!l
120
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
24 12 my $del_marker = "\0";
25 12 ▶ $format = 'CSV' if $format eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "ARRAY" 1 11 12
26 12 my $parser_name = 'AnyData/Format/' . $format . '.pm';
27 12 eval { require $parser_name; };
28 12 ▶ die "Error Opening File-Parser: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 12 12
29 12 $parser_name =~ s#/#::#g;
30 12 $parser_name =~ s#\.pm$##g;
31 12 ▶ my $col_names = $flags->{col_names} || undef;
Condition100%2/2
$flags->{'col_names'} || undef
l!l
75
MC/DC100%1/1
$flags->{'col_names'} || undef
$flags->{'col_names'}
Truth table100%2/2
A
A= $flags->{'col_names'}
Aresult
11
00
32 12 ▶ if ($col_names) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col_names) 7 5 12
33 7 my @cols;
34 7 ▶ @cols = ref $col_names eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
ref $col_names eq 'ARRAY' ? : 0 7 7
35 ? @$col_names
36 : split ',',$col_names;
37 7 $flags->{col_names} = \@cols;
38 }
39 12 $flags->{del_marker} = $del_marker;
40 12 ▶ $flags->{records} ||= $flags->{data};
Condition33%1/3
$flags->{"records"} ||= $flags->{"data"}
l!l&&r!l&&!r
0012
MC/DC0%0/2
$flags->{"records"} ||= $flags->{"data"}
$flags->{"records"} $flags->{"data"}
Truth table33%1/3
A ||= B
A= $flags->{"records"}B= $flags->{"data"}
ABresult
1X1
011
000
41 12 ▶ $flags->{field_sep} ||= $flags->{sep_char} ||= $flags->{ad_sep_char};
Condition33%2/6
$flags->{"field_sep"} ||= ($flags->{"sep_char"} ||= $flags->{"ad_sep_char"})
l!l&&r!l&&!r
0012
$flags->{"sep_char"} ||= $flags->{"ad_sep_char"}
l!l&&r!l&&!r
0012
MC/DC0%0/2
$flags->{"field_sep"} ||= ($flags->{"sep_char"} ||= $flags->{"ad_sep_char"})
$flags->{"field_sep"} ($flags->{"sep_char"} ||= $flags->{"ad_sep_char"})
MC/DC0%0/2
$flags->{"sep_char"} ||= $flags->{"ad_sep_char"}
$flags->{"sep_char"} $flags->{"ad_sep_char"}
Truth table33%1/3
A ||= B
A= $flags->{"field_sep"}B= ($flags->{"sep_char"} ||= $flags->{"ad_sep_char"})
ABresult
1X1
011
000
Truth table33%1/3
A ||= B
A= $flags->{"sep_char"}B= $flags->{"ad_sep_char"}
ABresult
1X1
011
000
42 12 ▶ $flags->{quote} ||= $flags->{quote_char} ||= $flags->{ad_quote_char};
Condition33%2/6
$flags->{"quote"} ||= ($flags->{"quote_char"} ||= $flags->{"ad_quote_char"})
l!l&&r!l&&!r
0012
$flags->{"quote_char"} ||= $flags->{"ad_quote_char"}
l!l&&r!l&&!r
0012
MC/DC0%0/2
$flags->{"quote"} ||= ($flags->{"quote_char"} ||= $flags->{"ad_quote_char"})
$flags->{"quote"} ($flags->{"quote_char"} ||= $flags->{"ad_quote_char"})
MC/DC0%0/2
$flags->{"quote_char"} ||= $flags->{"ad_quote_char"}
$flags->{"quote_char"} $flags->{"ad_quote_char"}
Truth table33%1/3
A ||= B
A= $flags->{"quote"}B= ($flags->{"quote_char"} ||= $flags->{"ad_quote_char"})
ABresult
1X1
011
000
Truth table33%1/3
A ||= B
A= $flags->{"quote_char"}B= $flags->{"ad_quote_char"}
ABresult
1X1
011
000
43 12 ▶ $flags->{escape} ||= $flags->{escape_char}||= $flags->{ad_escape_char};
Condition33%2/6
$flags->{"escape"} ||= ($flags->{"escape_char"} ||= $flags->{"ad_escape_char"})
l!l&&r!l&&!r
0012
$flags->{"escape_char"} ||= $flags->{"ad_escape_char"}
l!l&&r!l&&!r
0012
MC/DC0%0/2
$flags->{"escape"} ||= ($flags->{"escape_char"} ||= $flags->{"ad_escape_char"})
$flags->{"escape"} ($flags->{"escape_char"} ||= $flags->{"ad_escape_char"})
MC/DC0%0/2
$flags->{"escape_char"} ||= $flags->{"ad_escape_char"}
$flags->{"escape_char"} $flags->{"ad_escape_char"}
Truth table33%1/3
A ||= B
A= $flags->{"escape"}B= ($flags->{"escape_char"} ||= $flags->{"ad_escape_char"})
ABresult
1X1
011
000
Truth table33%1/3
A ||= B
A= $flags->{"escape_char"}B= $flags->{"ad_escape_char"}
ABresult
1X1
011
000
44 12 ▶ $flags->{record_sep}||= $flags->{eol} ||= $flags->{ad_eol};
Condition33%2/6
$flags->{"record_sep"} ||= ($flags->{"eol"} ||= $flags->{"ad_eol"})
l!l&&r!l&&!r
0012
$flags->{"eol"} ||= $flags->{"ad_eol"}
l!l&&r!l&&!r
0012
MC/DC0%0/2
$flags->{"record_sep"} ||= ($flags->{"eol"} ||= $flags->{"ad_eol"})
$flags->{"record_sep"} ($flags->{"eol"} ||= $flags->{"ad_eol"})
MC/DC0%0/2
$flags->{"eol"} ||= $flags->{"ad_eol"}
$flags->{"eol"} $flags->{"ad_eol"}
Truth table33%1/3
A ||= B
A= $flags->{"record_sep"}B= ($flags->{"eol"} ||= $flags->{"ad_eol"})
ABresult
1X1
011
000
Truth table33%1/3
A ||= B
A= $flags->{"eol"}B= $flags->{"ad_eol"}
ABresult
1X1
011
000
45 # $flags->{skip_first_row}
46 12 my $parser = $parser_name->new ($flags);
47 12 ▶ if ($parser->{col_names} && !$col_names) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parser->{"col_names"} and not $col_names) 1 11 12
Condition100%3/3
$parser->{"col_names"} and not $col_names
!ll&&!rl&&r
471
MC/DC100%2/2
$parser->{"col_names"} and not $col_names
$parser->{"col_names"} not $col_names
Truth table100%3/3
A and not(B)
A= $parser->{"col_names"}B= not $col_names
ABresult
0X0
100
111
48 1 my @cols;
49 @cols = ref $parser->{col_names} eq 'ARRAY'
50 0 ? @{$parser->{col_names}}
51 1 ▶ : split ',',$parser->{col_names};
Branch: 1 branch
BranchTrueFalseTotal
ref $parser->{'col_names'} eq 'ARRAY' ? : 0 1 1
52 1 $flags->{col_names} = \@cols;
53 1 $parser->{col_names} = \@cols;
54 }
55 my $storage_name = $flags->{storage}
56 12 ▶ || $parser->storage_type()
Condition100%5/5
$flags->{'storage'} || $parser->storage_type || 'File'
l!l
93
$flags->{"storage"} or $parser->storage_type
l!l&&r!l&&!r
813
MC/DC100%2/2
$flags->{'storage'} || $parser->storage_type || 'File'
$flags->{"storage"} $parser->storage_type
Truth table100%3/3
A or B
A= $flags->{"storage"}B= $parser->storage_type
ABresult
1X1
011
000
57 || 'File';
58 12 $storage_name = "AnyData/Storage/$storage_name.pm";
59 12 eval { require $storage_name; };
60 12 ▶ die "Error Opening Storage Module: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 12 12
61 12 $storage_name =~ s#/#::#g;
62 12 $storage_name =~ s#\.pm$##g;
63 12 my $storage = new $storage_name({del_marker=>$del_marker,%$flags});
64 12 ▶ if ($storage_name =~ 'PassThru') {
Branch: 1 branch
BranchTrueFalseTotal
if ($storage_name =~ /PassThru/) 0 12 12
65 0 $storage->{parser} = $parser;
66 0 $parser->{del_marker} = "\0";
67 $parser->{url} = $flags->{file}
68 0 ▶ if $flags->{file} and $flags->{file} =~ /http:|ftp:/;
Branch: 1 branch
BranchTrueFalseTotal
if $flags->{"file"} and $flags->{"file"} =~ /http:|ftp:/ 0 0 0
Condition0%0/3
$flags->{"file"} and $flags->{"file"} =~ /http:|ftp:/
!ll&&!rl&&r
000
MC/DC0%0/2
$flags->{"file"} and $flags->{"file"} =~ /http:|ftp:/
$flags->{"file"} $flags->{"file"} =~ /http:|ftp:/
Truth table0%0/3
A and B
A= $flags->{"file"}B= $flags->{"file"} =~ /http:|ftp:/
ABresult
0X0
100
111
69 }
70 12 my $self = {
71 storage => $storage,
72 parser => $parser,
73 };
74 12 return( bless($self,$class) );
75 }
76  
77 0 ▶sub adFormats {
Subroutine adFormats: not called CC=3 SCAR=24.8
Pod documented
78 0 my @formats;
79 0 for my $dir(@INC) {
80 0 my $format_dir = "$dir/AnyData/Format";
81 0 ▶ if ( -d $format_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $format_dir) 0 0 0
82 0 local *D;
83 0 opendir(D,$format_dir);
84 0 @formats = grep {/\.pm$/} readdir(D);
85 0 last;
86 }
87 }
88 0 unshift @formats,'ARRAY';
89 0 @formats = map {s/^(.*)\.pm$/$1/;$_} @formats;
90 0 return @formats;
91 }
92  
93 1 ▶sub export {
Subroutine export: called CC=2 SCAR=7.0
Pod undocumented
94 1 my $self=shift;
95 1 my $fh = $self->{storage}->{fh};
96 1 ▶ my $mode = $self->{storage}->{open_mode} || 'r';
Condition50%1/2
$self->{'storage'}{'open_mode'} || 'r'
l!l
10
MC/DC0%0/1
$self->{'storage'}{'open_mode'} || 'r'
$self->{'storage'}{'open_mode'}
Truth table50%1/2
A
A= $self->{'storage'}{'open_mode'}
Aresult
11
00
97 # if ( $self->{parser}->{export_on_close}
98 # && $self->{storage}->{fh}
99 # && $mode ne 'r'
100 # ){
101 1 return $self->{parser}->export( $self->{storage}, @_ );
102 # }
103 }
104 12 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
105 12 my $self=shift;
106 # $self->export;
107 12 $self->zpack;
108 #print "AD DESTROYED ";
109 }
110 ##########################################
111 # DBD STUFF
112 ##########################################
113 # required only for DBD-AnyData
114 ##########################################
115 0 ▶sub prep_dbd_table {
Subroutine prep_dbd_table: not called CC=6 SCAR=37.4
Pod undocumented
116 0 my $self = shift;
117 0 my $tname = shift;
118 0 my $createMode = shift;
119 0 my $col_names;
120 my $col_nums;
121 0 my $first_row_pos;
122 0 ▶ if (!$createMode) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($createMode) 0 0 0
123 0 $col_names = $self->{storage}->get_col_names($self->{parser});
124 0 $col_nums = $self->{storage}->set_col_nums();
125 0 $first_row_pos = $self->{storage}->{first_row_pos};
126 }
127 die "ERROR: No Column Names!:", $self->{storage}->{open_mode}
128 if (!$col_names || !scalar @$col_names)
129 && 'ru' =~ $self->{storage}->{open_mode}
130 0 ▶ && !$createMode eq 'o';
Branch: 1 branch
BranchTrueFalseTotal
if not $col_names && scalar @$col_names and "ru" =~ /$self->{"storage"}{"open_mode"}/ and !$createMode eq "o" 0 0 0
Condition0%0/9
not $col_names && scalar @$col_names and "ru" =~ /$self->{"storage"}{"open_mode"}/ and !$createMode eq "o"
!ll&&!rl&&r
000
not $col_names && scalar @$col_names and "ru" =~ /$self->{"storage"}{"open_mode"}/
!ll&&!rl&&r
000
$col_names && scalar @$col_names
!ll&&!rl&&r
000
MC/DC0%0/4
not $col_names && scalar @$col_names and "ru" =~ /$self->{"storage"}{"open_mode"}/ and !$createMode eq "o"
$col_names scalar @$col_names "ru" =~ /$self->{"storage"}{"open_mode"}/ !$createMode eq "o"
Truth table0%0/7
not(A && B) and C and not(D)
A= $col_namesB= scalar @$col_namesC= "ru" =~ /$self->{"storage"}{"open_mode"}/D= !$createMode eq "o"
ABCDresult
11XX0
0X0X0
100X0
0X100
10100
0X111
10111
131 my $table = {
132 NAME => $tname,
133 DATA => [],
134 CURRENT_ROW => 0,
135 col_names => $col_names,
136 col_nums => $col_nums,
137 first_row_pos => $first_row_pos,
138 fh => $self->{storage}->get_file_handle,
139 file => $self->{storage}->get_file_name,
140 0 ad => $self,
141 };
142 #use Data::Dumper; print Dumper $table;
143 0 return $table;
144 }
145 0 ▶sub fetch_row {
Subroutine fetch_row: not called CC=10 SCAR=47.0
Pod undocumented
146 0 my $self = shift;
147 0 ▶ my $requested_cols = shift || [];
Condition0%0/2
shift() || []
l!l
00
MC/DC0%0/1
shift() || []
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
148 0 my $rec;
149 0 ▶ if ( $self->{parser}->{skip_pattern} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'parser'}{'skip_pattern'}) { } 0 0 0
150 0 my $found;
151 0 ▶ while (!$found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 0 0 0
152 0 $rec = $self->{storage}->file2str($self->{parser},$requested_cols);
153 0 ▶ last if !defined $rec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rec 0 0 0
154 0 ▶ next if $rec =~ $self->{parser}->{skip_pattern};
Branch: 1 branch
BranchTrueFalseTotal
if $rec =~ /$self->{"parser"}{"skip_pattern"}/ 0 0 0
155 0 last;
156 }
157 }
158 else {
159 0 $rec = $self->{storage}->file2str($self->{parser},$requested_cols);
160 }
161 0 ▶ return $rec if ref $rec eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $rec eq "ARRAY" 0 0 0
162 0 ▶ return unless $rec;
Branch: 1 branch
BranchTrueFalseTotal
unless $rec 0 0 0
163 0 my @fields = $self->{parser}->read_fields($rec);
164 0 ▶ return undef if scalar @fields == 1 and !defined $fields[0];
Branch: 1 branch
BranchTrueFalseTotal
if scalar @fields == 1 and not defined $fields[0] 0 0 0
Condition0%0/3
scalar @fields == 1 and not defined $fields[0]
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @fields == 1 and not defined $fields[0]
scalar @fields == 1 not defined $fields[0]
Truth table0%0/3
A and not(B)
A= scalar @fields == 1B= not defined $fields[0]
ABresult
0X0
100
111
165 0 return \@fields;
166 }
167 0 ▶sub fetch_rowNEW {
Subroutine fetch_rowNEW: not called CC=10 SCAR=47.0
Pod undocumented
168 0 my $self = shift;
169 0 ▶ my $requested_cols = shift || [];
Condition0%0/2
shift() || []
l!l
00
MC/DC0%0/1
shift() || []
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
170 0 my $rec = $self->{storage}->file2str($self->{parser},$requested_cols);
171 0 my @fields;
172 0 ▶ if (ref $rec eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rec eq 'ARRAY') { } 0 0 0
173 0 @fields = @$rec;
174 }
175 else {
176 0 ▶ return unless defined $rec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rec 0 0 0
177 0 my @fields = $self->{parser}->read_fields($rec);
178 0 ▶ return undef if scalar @fields == 1 and !defined $fields[0];
Branch: 1 branch
BranchTrueFalseTotal
if scalar @fields == 1 and not defined $fields[0] 0 0 0
Condition0%0/3
scalar @fields == 1 and not defined $fields[0]
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @fields == 1 and not defined $fields[0]
scalar @fields == 1 not defined $fields[0]
Truth table0%0/3
A and not(B)
A= scalar @fields == 1B= not defined $fields[0]
ABresult
0X0
100
111
179 }
180 0 ▶ if ( my $subs = $self->{parser}->{read_sub} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $subs = $self->{"parser"}{"read_sub"}) 0 0 0
181 0 for (@$subs) {
182 0 my($col,$sub) = @$_;
183 0 ▶ next unless defined $col;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $col 0 0 0
184 0 my $col_num = $self->{storage}->{col_nums}->{$col};
185 0 ▶ next unless defined $col_num;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $col_num 0 0 0
186 0 $fields[$col_num] = &$sub($fields[$col_num]);
187 }
188 }
189 0 return \@fields;
190 }
191 0 ▶sub push_names {
Subroutine push_names: not called CC=5 SCAR=34.0
Pod undocumented
192 0 my $self = shift;
193 0 ▶ my $col_names = shift || undef;
Condition0%0/2
shift() || undef
l!l
00
MC/DC0%0/1
shift() || undef
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
194 #print "Can't find column names!" unless scalar @$col_names;
195 $self->{storage}->print_col_names( $self->{parser}, $col_names )
196 0 ▶ unless $self->{parser}->{col_names} && $self->parser_type ne 'XML';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"parser"}{"col_names"} and $self->parser_type ne "XML" 0 0 0
Condition0%0/3
$self->{"parser"}{"col_names"} and $self->parser_type ne "XML"
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"parser"}{"col_names"} and $self->parser_type ne "XML"
$self->{"parser"}{"col_names"} $self->parser_type ne "XML"
Truth table0%0/3
A and B
A= $self->{"parser"}{"col_names"}B= $self->parser_type ne "XML"
ABresult
0X0
100
111
197 # $self->set_col_nums;
198 0 ▶ $self->{parser}->{key} ||= $col_names->[0];
Condition0%0/3
$self->{"parser"}{"key"} ||= $col_names->[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"parser"}{"key"} ||= $col_names->[0]
$self->{"parser"}{"key"} $col_names->[0]
Truth table0%0/3
A ||= B
A= $self->{"parser"}{"key"}B= $col_names->[0]
ABresult
1X1
011
000
199 #use Data::Dumper; print Dumper $self; exit;
200 }
201 0 ▶sub drop { shift->{storage}->drop(@_); }
Subroutine drop: not called CC=1 SCAR=6.9
Pod undocumented
202 0 ▶sub truncate { shift->{storage}->truncate(@_) }
Subroutine truncate: not called CC=1 SCAR=6.9
Pod undocumented
203  
204 ##################################################################
205 # END OF DBD STUFF
206 ##################################################################
207  
208 ##################################################################
209 # REQUIRED BY BOTH DBD AND TIEDHASH
210 ##################################################################
211 42 ▶sub push_row {
Subroutine push_row: called CC=4 SCAR=14.0
Pod undocumented
212 42 my $self = shift;
213 42 ▶ die "ERROR: No Column Names!" unless scalar @{$self->col_names};
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$self->col_names;} 0 42 42
214 42 my $requested_cols = [];
215 42 my @row = @_;
216 42 ▶ if (ref($row[0]) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $row[0] eq "ARRAY") 6 36 42
217 6 $requested_cols = shift @row;
218 }
219 42 ▶ my $rec = $self->{parser}->write_fields(@row) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $rec = $self->{"parser"}->write_fields(@row) or return undef 42 0 42
220 42 return $self->{storage}->push_row( $rec, $self->{parser}, $requested_cols);
221 }
222 0 ▶sub push_rowNEW {
Subroutine push_rowNEW: not called CC=4 SCAR=25.8
Pod undocumented
223 0 my $self = shift;
224 #print "PUSHING... ";
225 0 ▶ die "ERROR: No Column Names!" unless scalar @{$self->col_names};
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$self->col_names;} 0 0 0
226 0 my $requested_cols = [];
227 0 my @row = @_;
228 6 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
229 #print "PUSHING ", Dumper \@row;
230 0 ▶ if (ref($row[0]) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $row[0] eq "ARRAY") 0 0 0
231 0 $requested_cols = shift @row;
232 }
233 0 ▶ my $rec = $self->{parser}->write_fields(@row) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $rec = $self->{"parser"}->write_fields(@row) or return undef 0 0 0
234 0 return $self->{storage}->push_row( $rec, $self->{parser}, $requested_cols);
235 }
236 30 ▶sub seek { shift->{storage}->seek(@_); }
Subroutine seek: called CC=1 SCAR=0.0
Pod undocumented
237 54 ▶sub seek_first_record {
Subroutine seek_first_record: called CC=1 SCAR=0.0
Pod undocumented
238 54 my $self=shift;
239 54 $self->{storage}->seek_first_record($self->{parser});
240 }
241 290 ▶sub col_names {
Subroutine col_names: called CC=4 SCAR=14.9
Pod undocumented
242 290 my $self = shift;
243 290 my $c = $self->{storage}->{col_names};
244 290 ▶ $c = $self->{parser}->{col_names} unless (ref $c eq 'ARRAY') and scalar @$c;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $c eq "ARRAY" and scalar @$c 0 290 290
Condition50%1/2
ref $c eq "ARRAY" and scalar @$c
!ll
0290
MC/DC0%0/1
ref $c eq "ARRAY" and scalar @$c
ref $c eq "ARRAY"
Truth table50%1/2
A
A= ref $c eq "ARRAY"
Aresult
00
11
245 290 ▶ $c ||= [];
Condition50%1/2
$c ||= []
l!l
2900
MC/DC0%0/1
$c ||= []
$c
Truth table50%1/2
A
A= $c
Aresult
11
00
246 }
247 11 ▶sub is_url {
Subroutine is_url: called CC=3 SCAR=13.1
Pod undocumented
248 11 my $file = shift;
249 11 ▶ return $file if $file and $file =~ m"^http://|ftp://";
Branch: 1 branch
BranchTrueFalseTotal
if $file and $file =~ m[^http://|ftp://] 0 11 11
Condition33%1/3
$file and $file =~ m[^http://|ftp://]
!ll&&!rl&&r
0110
MC/DC0%0/2
$file and $file =~ m[^http://|ftp://]
$file $file =~ m[^http://|ftp://]
Truth table33%1/3
A and B
A= $fileB= $file =~ m[^http://|ftp://]
ABresult
0X0
100
111
250 }
251  
252 11 ▶sub adTable {
Subroutine adTable: called CC=25 SCAR=41.1
Pod undocumented
253 ###########################################################
254 # Patch from Wes Hardaker
255 ###########################################################
256 # my($formatref,$file,$read_mode,$lockMode,$othflags)=@_;
257 11 my($formatref,$file,$read_mode,$lockMode,$othflags,$tname)=@_;
258 ###########################################################
259 #use Data::Dumper; print Dumper \@_;
260 11 my($format,$flags);
261 11 ▶ $file ||= '';
Condition50%1/2
$file ||= ""
l!l
110
MC/DC0%0/1
$file ||= ""
$file
Truth table50%1/2
A
A= $file
Aresult
11
00
262 11 my $url = is_url($file);
263 11 $flags = {};
264 11 ▶ $othflags ||= {};
Condition100%2/2
$othflags ||= {}
l!l
92
MC/DC100%1/1
$othflags ||= {}
$othflags
Truth table100%2/2
A
A= $othflags
Aresult
11
00
265 11 ▶ if ( ref $formatref eq 'HASH' or $othflags->{data}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $formatref eq 'HASH' or $othflags->{'data'}) { } 0 11 11
Condition33%1/3
ref $formatref eq "HASH" or $othflags->{"data"}
l!l&&r!l&&!r
0011
MC/DC0%0/2
ref $formatref eq "HASH" or $othflags->{"data"}
ref $formatref eq "HASH" $othflags->{"data"}
Truth table33%1/3
A or B
A= ref $formatref eq "HASH"B= $othflags->{"data"}
ABresult
1X1
011
000
266 0 $format = 'Base';
267 0 $flags = $othflags;
268 0 ▶ if (ref $formatref eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $formatref eq "HASH") 0 0 0
269 0 %$flags = (%$formatref,%$othflags);
270 }
271 }
272 else {
273 11 ($format,$flags) = split_params($formatref);
274 11 ▶ $othflags ||= {};
Condition50%1/2
$othflags ||= {}
l!l
110
MC/DC0%0/1
$othflags ||= {}
$othflags
Truth table50%1/2
A
A= $othflags
Aresult
11
00
275 11 %$flags = (%$flags,%$othflags);
276 }
277 11 ▶ if ( $flags->{cols} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags->{"cols"}) 6 5 11
278 6 $flags->{col_names} = $flags->{cols};
279 6 delete $flags->{cols};
280 }
281 11 ▶ if (ref($file) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file eq 'ARRAY') { } 8 3 11
282 8 ▶ if ($format eq 'Mp3' or $format eq 'FileSys') {
Branch: 1 branch
BranchTrueFalseTotal
if ($format eq 'Mp3' or $format eq 'FileSys') { } 0 8 8
Condition33%1/3
$format eq "Mp3" or $format eq "FileSys"
l!l&&r!l&&!r
008
MC/DC0%0/2
$format eq "Mp3" or $format eq "FileSys"
$format eq "Mp3" $format eq "FileSys"
Truth table33%1/3
A or B
A= $format eq "Mp3"B= $format eq "FileSys"
ABresult
1X1
011
000
283 0 $flags->{dirs} = $file;
284 }
285 else {
286 8 $flags->{recs} = join '',@$file;
287 8 ▶ $flags->{recs} = $file if $format =~ /ARRAY/i;
Branch: 1 branch
BranchTrueFalseTotal
if $format =~ /ARRAY/i 1 7 8
288 8 ▶ $flags->{storage} = 'RAM' unless $format eq 'XML';
Branch: 1 branch
BranchTrueFalseTotal
unless $format eq "XML" 8 0 8
289 8 $read_mode = 'u';
290 }
291 }
292 else {
293 3 $flags->{file} = $file;
294 }
295 11 ▶ if ($format ne 'XML' and ($format eq 'Base' or $url) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format ne "XML" and $format eq "Base" || $url) 0 11 11
Condition33%2/6
$format ne "XML" and $format eq "Base" || $url
!ll&&!rl&&r
0110
$format eq "Base" or $url
l!l&&r!l&&!r
0011
MC/DC0%0/3
$format ne "XML" and $format eq "Base" || $url
$format ne "XML" $format eq "Base" $url
Truth table25%1/4
A and B or C
A= $format ne "XML"B= $format eq "Base"C= $url
ABCresult
0XX0
1000
11X1
1011
296 0 my $x;
297 0 $flags->{storage} = 'RAM';
298 0 delete $flags->{recs};
299 0 my $ad = AnyData->new( $format, $flags);
300 $format eq 'Base'
301 ? $ad->open_table( $file )
302 : $ad->open_table( $file, 'r',
303 0 ▶ $ad->{storage}->get_remote_data($file)
Branch: 1 branch
BranchTrueFalseTotal
$format eq 'Base' ? : 0 0 0
304 );
305 0 return $ad;
306 }
307 11 my $ad = AnyData->new( $format, $flags);
308 11 my $createMode = 0;
309 11 ▶ $createMode = $read_mode if defined $lockMode;
Branch: 1 branch
BranchTrueFalseTotal
if defined $lockMode 0 11 11
310 11 ▶ $read_mode = 'c' if $createMode and $lockMode;
Branch: 1 branch
BranchTrueFalseTotal
if $createMode and $lockMode 0 11 11
Condition33%1/3
$createMode and $lockMode
!ll&&!rl&&r
1100
MC/DC0%0/2
$createMode and $lockMode
$createMode $lockMode
Truth table33%1/3
A and B
A= $createModeB= $lockMode
ABresult
0X0
100
111
311 11 ▶ $read_mode = 'u' if !$createMode and $lockMode;
Branch: 1 branch
BranchTrueFalseTotal
if not $createMode and $lockMode 0 11 11
Condition33%1/3
not $createMode and $lockMode
!ll&&!rl&&r
0110
MC/DC0%0/2
not $createMode and $lockMode
not $createMode $lockMode
Truth table33%1/3
not(A) and B
A= not $createModeB= $lockMode
ABresult
0X0
100
111
312 11 ▶ $read_mode ||= 'r';
Condition50%1/2
$read_mode ||= "r"
l!l
110
MC/DC0%0/1
$read_mode ||= "r"
$read_mode
Truth table50%1/2
A
A= $read_mode
Aresult
11
00
313 $ad->{parser}->{keep_first_line} = 1
314 11 ▶ if $flags->{col_names} and 'ru' =~ /$read_mode/;
Branch: 1 branch
BranchTrueFalseTotal
if $flags->{"col_names"} and "ru" =~ /$read_mode/ 7 4 11
Condition100%3/3
$flags->{"col_names"} and "ru" =~ /$read_mode/
!ll&&!rl&&r
317
MC/DC100%2/2
$flags->{"col_names"} and "ru" =~ /$read_mode/
$flags->{"col_names"} "ru" =~ /$read_mode/
Truth table100%3/3
A and B
A= $flags->{"col_names"}B= "ru" =~ /$read_mode/
ABresult
0X0
100
111
315 #####################################################
316 # Patch from Wes Hardaker
317 #####################################################
318 # $ad->open_table( $file, $read_mode );
319 ## $ad->open_table( $file, $read_mode, $tname );
320 11 $ad->open_table( $file, $read_mode, $tname );
321 # use Data::Dumper; my $x = $ad; delete $x->{parser}->{twig}; delete $x->{parser}->{record_tag}; delete $x->{parser}->{current_element}; print Dumper $x;
322 #####################################################
323 11 return $ad;
324 }
325  
326 11 ▶sub open_table {
Subroutine open_table: called CC=4 SCAR=14.0
Pod undocumented
327 11 my $self = shift;
328 11 $self->{storage}->open_table( $self->{parser}, @_ );
329 11 my $col_names = $self->col_names();
330 11 ▶ $self->{parser}->{key} ||= '';
Condition100%2/2
$self->{"parser"}{"key"} ||= ""
l!l
110
MC/DC100%1/1
$self->{"parser"}{"key"} ||= ""
$self->{"parser"}{"key"}
Truth table100%2/2
A
A= $self->{"parser"}{"key"}
Aresult
11
00
331 11 ▶ $self->{parser}->{key} ||= $col_names->[0] if $col_names->[0];
Branch: 1 branch
BranchTrueFalseTotal
if $col_names->[0] 11 0 11
Condition66%2/3
$self->{"parser"}{"key"} ||= $col_names->[0]
l!l&&r!l&&!r
1100
MC/DC0%0/2
$self->{"parser"}{"key"} ||= $col_names->[0]
$self->{"parser"}{"key"} $col_names->[0]
Truth table66%2/3
A ||= B
A= $self->{"parser"}{"key"}B= $col_names->[0]
ABresult
1X1
011
000
332 }
333 ##################################################################
334  
335  
336 ##################################################################
337 # TIEDHASH STUFF
338 ##################################################################
339 98 ▶sub key_col { shift->{parser}->{key} }
Subroutine key_col: called CC=1 SCAR=0.0
Pod undocumented
340  
341 221 ▶sub fetchrow_hashref {
Subroutine fetchrow_hashref: called CC=6 SCAR=18.1
Pod undocumented
342 221 my $self = shift;
343 221 ▶ my $rec = $self->get_undeleted_record or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $rec = $self->get_undeleted_record or return undef 186 35 221
344 my @fields = ref $rec eq 'ARRAY'
345 ? @$rec
346 186 ▶ : $self->{parser}->read_fields($rec);
Branch: 1 branch
BranchTrueFalseTotal
ref $rec eq 'ARRAY' ? : 162 24 186
347 186 my $col_names = $self->col_names();
348 186 ▶ return undef unless scalar @fields;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @fields 0 186 186
349 186 ▶ return undef if scalar @fields == 1 and !defined $fields[0];
Branch: 1 branch
BranchTrueFalseTotal
if scalar @fields == 1 and not defined $fields[0] 0 186 186
Condition66%2/3
scalar @fields == 1 and not defined $fields[0]
!ll&&!rl&&r
18330
MC/DC0%0/2
scalar @fields == 1 and not defined $fields[0]
scalar @fields == 1 not defined $fields[0]
Truth table66%2/3
A and not(B)
A= scalar @fields == 1B= not defined $fields[0]
ABresult
0X0
100
111
350 186 my $rowhash;
351 186 @{$rowhash}{@$col_names} = @fields;
352 186 return ( $rowhash );
353 }
354 235 ▶sub get_undeleted_record {
Subroutine get_undeleted_record: called CC=7 SCAR=20.1
Pod undocumented
355 235 my $self = shift;
356 235 my $rec;
357 235 my $found=0;
358 235 ▶ return $self->fetch_row if $self->parser_type eq 'XML';
Branch: 1 branch
BranchTrueFalseTotal
if $self->parser_type eq "XML" 0 235 235
359 235 ▶ while (!$found) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 289 0 289
360 289 my $test = $rec = $self->{storage}->file2str($self->{parser});
361 289 ▶ return if !defined $rec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rec 37 252 289
362 252 ▶ next if $self->{storage}->is_deleted($self->{parser});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"storage"}->is_deleted($self->{"parser"}) 54 198 252
363 next if $self->{parser}->{skip_pattern}
364 198 ▶ and $rec =~ $self->{parser}->{skip_pattern};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parser"}{"skip_pattern"} and $rec =~ /$self->{"parser"}{"skip_pattern"}/ 0 198 198
Condition33%1/3
$self->{"parser"}{"skip_pattern"} and $rec =~ /$self->{"parser"}{"skip_pattern"}/
!ll&&!rl&&r
19800
MC/DC0%0/2
$self->{"parser"}{"skip_pattern"} and $rec =~ /$self->{"parser"}{"skip_pattern"}/
$self->{"parser"}{"skip_pattern"} $rec =~ /$self->{"parser"}{"skip_pattern"}/
Truth table33%1/3
A and B
A= $self->{"parser"}{"skip_pattern"}B= $rec =~ /$self->{"parser"}{"skip_pattern"}/
ABresult
0X0
100
111
365 198 last;
366 }
367 198 return $rec;
368 # return $rec if ref $rec eq 'ARRAY';
369 # return unless $rec;
370 # my @fields = $self->{parser}->read_fields($rec);
371 # return undef if scalar @fields == 1 and !defined $fields[0];
372 # return \@fields;
373 }
374 6 ▶sub update_single_row {
Subroutine update_single_row: called CC=4 SCAR=13.9
Pod undocumented
375 6 my $self = shift;
376 6 my $oldrow = shift;
377 6 my $newvals = shift;
378 6 my @colnames = @{ $self->col_names };
379 6 my @newrow;
380 6 my $requested_cols = [];
381 6 for my $i(0..$#colnames) {
382 18 ▶ push @$requested_cols, $colnames[$i] if defined $newvals->{$colnames[$i]};
Branch: 1 branch
BranchTrueFalseTotal
if defined $$newvals{$colnames[$i]} 6 12 18
383 18 $newrow[$i] = $newvals->{$colnames[$i]};
384 18 ▶ $newrow[$i] = $oldrow->{$colnames[$i]} unless defined $newrow[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $newrow[$i] 12 6 18
385 }
386 6 unshift @newrow, $requested_cols;
387 6 $self->{storage}->seek(0,2);
388 6 $self->push_row( @newrow );
389 6 return \@newrow;
390 }
391 6 ▶sub update_multiple_rows {
Subroutine update_multiple_rows: called CC=7 SCAR=19.6
Pod undocumented
392 6 my $self = shift;
393 6 my $key = shift;
394 6 my $values = shift;
395 6 $self->seek_first_record;
396 6 my @rows_to_update;
397 6 ▶ while (my $row = $self->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $self->fetchrow_hashref) 30 6 36
398 30 ▶ next unless $self->match($row,$key);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->match($row, $key) 24 6 30
399 $self->{parser}->{has_update_function}
400 6 ▶ ? $self->update_single_row($row,$values)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'parser'}{'has_update_function'} ? : 0 6 6
401 : $self->delete_single_row();
402 $self->{parser}->{has_update_function}
403 6 ▶ ? push @rows_to_update,1
Branch: 1 branch
BranchTrueFalseTotal
$self->{'parser'}{'has_update_function'} ? : 0 6 6
404 : push @rows_to_update,$row;
405 }
406 6 ▶ if (!$self->{parser}->{has_update_function}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"parser"}{"has_update_function"}) 6 0 6
407 6 for (@rows_to_update) {
408 6 $self->update_single_row($_,$values);
409 }
410 }
411 6 return scalar @rows_to_update;
412 }
413 107 ▶sub match {
Subroutine match: called CC=8 SCAR=20.8
Pod undocumented
414 107 my($self,$row,$key) = @_;
415 107 ▶ if ( ref $key ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $key ne "HASH") 34 73 107
416 return 0 if !$row->{$self->key_col}
417 34 ▶ or $row->{$self->key_col} ne $key;
Branch: 1 branch
BranchTrueFalseTotal
if not $$row{$self->key_col} or $$row{$self->key_col} ne $key 18 16 34
Condition66%2/3
not $$row{$self->key_col} or $$row{$self->key_col} ne $key
l!l&&r!l&&!r
01816
MC/DC50%1/2
not $$row{$self->key_col} or $$row{$self->key_col} ne $key
not $$row{$self->key_col} $$row{$self->key_col} ne $key
Truth table66%2/3
not(A) or B
A= not $$row{$self->key_col}B= $$row{$self->key_col} ne $key
ABresult
1X1
011
000
418 16 return 1;
419 }
420 73 my $found = 0;
421 73 ▶ while (my($col,$re)=each %$key) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$key) 30 73 103
422 30 ▶ next unless defined $row->{$col} and is_matched($row->{$col},$re);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row->{$col} and is_matched($row->{$col}, $re) 24 6 30
Condition66%2/3
defined $row->{$col} and is_matched($row->{$col}, $re)
!ll&&!rl&&r
0246
MC/DC50%1/2
defined $row->{$col} and is_matched($row->{$col}, $re)
defined $row->{$col} is_matched($row->{$col}, $re)
Truth table66%2/3
A and B
A= defined $row->{$col}B= is_matched($row->{$col}, $re)
ABresult
0X0
100
111
423 6 $found++;
424 }
425 73 ▶ return 1 if $found == scalar keys %$key;
Branch: 1 branch
BranchTrueFalseTotal
if $found == scalar keys %$key 49 24 73
426 }
427 30 ▶sub is_matched {
Subroutine is_matched: called CC=23 SCAR=59.1
Pod undocumented
428 30 my($str,$re)=@_;
429 30 ▶ if (ref $re eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $re eq "Regexp") 0 30 30
430 0 ▶ return $str =~ /$re/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$str =~ /$re/ ? : 0 0 0
431 }
432 30 my($op,$val);
433
434 30 ▶ if ( $re and $re =~/^(\S*)\s+(.*)/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($re and $re =~ /^(\S*)\s+(.*)/) { } 0 30 30
elsif ($re) { } 30 0 30
Condition33%1/3
$re and $re =~ /^(\S*)\s+(.*)/
!ll&&!rl&&r
0300
MC/DC0%0/2
$re and $re =~ /^(\S*)\s+(.*)/
$re $re =~ /^(\S*)\s+(.*)/
Truth table33%1/3
A and B
A= $reB= $re =~ /^(\S*)\s+(.*)/
ABresult
0X0
100
111
435 0 $op = $1;
436 0 $val = $2;
437 }
438 elsif ($re) {
439 30 ▶ return $str =~ /$re/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$str =~ /$re/ ? : 6 24 30
440 }
441 else {
442 0 ▶ return $str eq '' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$str eq '' ? : 0 0 0
443 }
444 0 my $numop = '< > == != <= >=';
445 0 my $chrop = 'lt gt eq ne le ge';
446 0 ▶ if (!($numop =~ /$op/) and !($chrop =~ /$op/)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($numop =~ /$op/ or $chrop =~ /$op/) 0 0 0
Condition0%0/3
$numop =~ /$op/ or $chrop =~ /$op/
l!l&&r!l&&!r
000
MC/DC0%0/2
$numop =~ /$op/ or $chrop =~ /$op/
$numop =~ /$op/ $chrop =~ /$op/
Truth table0%0/3
A or B
A= $numop =~ /$op/B= $chrop =~ /$op/
ABresult
1X1
011
000
447 0 ▶ return $str =~ /$re/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$str =~ /$re/ ? : 0 0 0
448 }
449 0 ▶ if ($op eq '<' ) { return $str < $val; }
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "<") 0 0 0
450 0 ▶ if ($op eq '>' ) { return $str > $val; }
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq ">") 0 0 0
451 0 ▶ if ($op eq '==') { return $str == $val; }
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "==") 0 0 0
452 0 ▶ if ($op eq '!=') { return $str != $val; }
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "!=") 0 0 0
453 0 ▶ if ($op eq '<=') { return $str <= $val; }
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "<=") 0 0 0
454 0 ▶ if ($op eq '>=') { return $str >= $val; }
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq ">=") 0 0 0
455 0 ▶ if ($op eq 'lt') { return $str lt $val; }
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "lt") 0 0 0
456 0 ▶ if ($op eq 'gt') { return $str gt $val; }
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "gt") 0 0 0
457 0 ▶ if ($op eq 'eq') { return $str eq $val; }
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "eq") 0 0 0
458 0 ▶ if ($op eq 'ne') { return $str ne $val; }
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "ne") 0 0 0
459 0 ▶ if ($op eq 'le') { return $str le $val; }
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "le") 0 0 0
460 0 ▶ if ($op eq 'ge') { return $str ge $val; }
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "ge") 0 0 0
461 }
462 12 ▶sub delete_single_row {
Subroutine delete_single_row: called CC=1 SCAR=0.0
Pod undocumented
463 12 my $self = shift;
464 # my $curpos = $self->{storage}->get_pos;
465 12 $self->{storage}->delete_record($self->{parser});
466 # $self->{storage}->go_pos($curpos);
467 12 $self->{needs_packing}++;
468 }
469 0 ▶sub delete_multiple_rows {
Subroutine delete_multiple_rows: not called CC=3 SCAR=24.8
Pod undocumented
470 0 my $self = shift;
471 0 my $key = shift;
472 0 $self->seek_first_record;
473 0 my $rows_deleted =0;
474 0 ▶ while (my $row = $self->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $self->fetchrow_hashref) 0 0 0
475 0 ▶ next unless $self->match($row,$key);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->match($row, $key) 0 0 0
476 0 $self->delete_single_row;
477 0 $rows_deleted++;
478 }
479 0 return $rows_deleted;
480 }
481  
482 6 ▶sub adNames { @{ shift->{__colnames}} }
Subroutine adNames: called CC=1 SCAR=0.0
Pod documented
483  
484 1 ▶sub adDump {
Subroutine adDump: called CC=5 SCAR=16.1
Pod documented
485 1 my $table = shift;
486 1 my $pat = shift;
487 1 ▶ die "No table defined" unless $table;
Branch: 1 branch
BranchTrueFalseTotal
unless $table 0 1 1
488 1 my $ad = tied(%$table)->{ad};
489 1 my @cols = @{ $ad->col_names };
490 1 print "<",join(":", @cols), ">\n";
491 1 ▶ while (my $row = each %$table) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $row = each %$table)) 6 1 7
492 6 ▶ my @row = map {defined $row->{$_} ? $row->{$_} : ''} @cols;
Branch: 1 branch
BranchTrueFalseTotal
defined $row->{$_} ? : 10 2 12
493 6 for (@row) { print "[$_]"; }
494 6 print "\n";
495 }
496 }
497  
498 10 ▶sub adRows {
Subroutine adRows: called CC=1 SCAR=0.0
Pod documented
499 10 my $thash = shift;
500 10 my %keys = @_;
501 10 my $obj = tied(%$thash);
502 10 return $obj->adRows(\%keys);
503 }
504 12 ▶sub adColumn {
Subroutine adColumn: called CC=1 SCAR=0.0
Pod documented
505 12 my $thash = shift;
506 12 my $column = shift;
507 12 my $flags = shift;
508 12 my $obj = tied(%$thash);
509 12 return $obj->adColumn($column, $flags);
510 }
511 0 ▶sub adArray {
Subroutine adArray: not called CC=1 SCAR=6.9
Pod undocumented
512 0 my($format,$data)=@_;
513 0 my $t = adTie( $format, $data );
514 0 my $t1 = tied(%$t);
515 0 my $ad = $t1->{ad};
516 0 my $arrayref = $ad->{storage}->{records};
517 0 unshift @$arrayref, $ad->{storage}->{col_names};
518 0 return $arrayref;
519 }
520 ##################################################################
521 # END OF TIEDHASH STUFF
522 ##################################################################
523 235 ▶sub parser_type {
Subroutine parser_type: called CC=1 SCAR=0.0
Pod undocumented
524 235 my $type = ref shift->{parser};
525 235 $type =~ s/AnyData::Format::(.*)/$1/;
526 235 return $type;
527 }
528 12 ▶sub zpack {
Subroutine zpack: called CC=10 SCAR=40.8
Pod undocumented
529 12 my $self = shift;
530 12 ▶ return if $self->{storage}->{no_pack};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"storage"}{"no_pack"} 0 12 12
531 12 ▶ return if (ref $self->{storage} ) !~ /File$/;
Branch: 1 branch
BranchTrueFalseTotal
unless ref($self->{"storage"}) =~ /File$/ 9 3 12
532  
533 # return unless $self->{needs_packing};
534 # $self->{needs_packing} = 0;
535 3 ▶ return unless scalar(keys %{ $self->{storage}->{deleted} } );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %{$$self{"storage"}{"deleted"};} 3 0 3
536 0 $self->{needs_packing} = 0;
537 # my @callA = caller 2;
538 # my @callB = caller 3;
539 # return if $callA[3] =~ /DBD/;
540 # return if $callB[3] and $callB[3] =~ /SQL::Statement/;
541 # return if $self->{parser}->{export_on_close};
542 #print "PACKING";
543 0 my $bak_file = $self->{storage}->get_file_name . '.bak';
544 0 my $bak = adTable( 'Text', $bak_file, 'o' );
545 0 my $bak_fh = $bak->{storage}->get_file_handle;
546 0 my $fh = $self->{storage}->get_file_handle;
547 0 ▶ die "Can't pack to backup $!" unless $fh and $bak_fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh and $bak_fh 0 0 0
Condition0%0/3
$fh and $bak_fh
!ll&&!rl&&r
000
MC/DC0%0/2
$fh and $bak_fh
$fh $bak_fh
Truth table0%0/3
A and B
A= $fhB= $bak_fh
ABresult
0X0
100
111
548 # $self->seek_first_record;
549 0 ▶ $fh->seek(0,0) || die $!;
Branch: 1 branch
BranchTrueFalseTotal
$fh->seek(0, 0) or die $! 0 0 0
550 #$bak_fh->seek(0,0) || die $!;
551 # while (my $line = $self->get_record) {
552 # next if $self->is_deleted($line);
553 0 ▶ while (my $line = $self->get_undeleted_record) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $self->get_undeleted_record) 0 0 0
554 my $tmpstr = $bak->{parser}->write_fields($line)
555 0 . $self->{parser}->{record_sep};
556 0 $bak_fh->write($tmpstr,length $tmpstr);
557 }
558 0 $fh->seek(0,0);
559 0 ▶ $fh->truncate(0) || die $!;
Branch: 1 branch
BranchTrueFalseTotal
$fh->truncate(0) or die $! 0 0 0
560 0 $bak->seek_first_record;
561 0 ▶ while (<$bak_fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $bak_fh)) 0 0 0
562 0 $fh->write($_,length $_);
563 }
564 0 $fh->close;
565 0 $bak_fh->close;
566 0 $self->{doing_pack} = 0;
567 0 undef $self->{storage}->{deleted};
568 }
569  
570 ##########################################################
571 # FUNCTION CALL INTERFACE
572 ##########################################################
573 10 ▶sub adTie {
Subroutine adTie: called CC=4 SCAR=14.4
Pod documented
574 10 my($format,$file,$read_mode,$flags)=@_;
575 10 my $data;
576 10 ▶ if (ref $file eq 'ARRAY' && !$read_mode ) { $read_mode = 'u'; }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file eq "ARRAY" and not $read_mode) 2 8 10
Condition100%3/3
ref $file eq "ARRAY" and not $read_mode
!ll&&!rl&&r
262
MC/DC100%2/2
ref $file eq "ARRAY" and not $read_mode
ref $file eq "ARRAY" not $read_mode
Truth table100%3/3
A and not(B)
A= ref $file eq "ARRAY"B= not $read_mode
ABresult
0X0
100
111
577 # ARRAY only {data=>[]};
578 10 ▶ if (scalar @_ == 1){
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1) 0 10 10
579 0 $read_mode = 'o';
580 0 tie %$data,
581 'AnyData::Storage::TiedHash',
582 adTable($format),
583 $read_mode;
584 0 return $data;
585 }
586 10 tie %$data,
587 'AnyData::Storage::TiedHash',
588 adTable($format,$file,$read_mode,undef,$flags),
589 $read_mode;
590 10 return $data;
591 }
592 0 ▶sub adErr {
Subroutine adErr: not called CC=3 SCAR=24.8
Pod undocumented
593 0 my $hash = shift;
594 0 my $t = tied(%$hash);
595 my $errstr = $t->{ad}->{parser}->{errstr}
596 0 ▶ || $t->{ad}->{storage}->{errstr};
Condition0%0/3
$t->{'ad'}{'parser'}{'errstr'} || $t->{'ad'}{'storage'}{'errstr'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$t->{'ad'}{'parser'}{'errstr'} || $t->{'ad'}{'storage'}{'errstr'}
$t->{'ad'}{'parser'}{'errstr'} $t->{'ad'}{'storage'}{'errstr'}
Truth table0%0/3
A || B
A= $t->{'ad'}{'parser'}{'errstr'}B= $t->{'ad'}{'storage'}{'errstr'}
ABresult
1X1
011
000
597 0 ▶ print $errstr if $errstr;
Branch: 1 branch
BranchTrueFalseTotal
if $errstr 0 0 0
598 0 return $errstr;
599 }
600 2 ▶sub adExport {
Subroutine adExport: called CC=4 SCAR=14.8
Pod documented
601 2 my $tiedhash = shift;
602 2 my($tformat,$tfile,$tflags)=@_;
603 2 my $ad = tied(%$tiedhash)->{ad};
604 2 my $sformat = ref $ad->{parser};
605 2 $sformat =~ s/AnyData::Format:://;
606 2 ▶ $tformat ||= $sformat;
Condition33%1/3
$tformat ||= $sformat
l!l&&r!l&&!r
200
MC/DC0%0/2
$tformat ||= $sformat
$tformat $sformat
Truth table33%1/3
A ||= B
A= $tformatB= $sformat
ABresult
1X1
011
000
607 2 ▶ if ($tformat eq $sformat and $tformat eq 'XML') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tformat eq $sformat and $tformat eq "XML") 0 2 2
Condition66%2/3
$tformat eq $sformat and $tformat eq "XML"
!ll&&!rl&&r
110
MC/DC0%0/2
$tformat eq $sformat and $tformat eq "XML"
$tformat eq $sformat $tformat eq "XML"
Truth table66%2/3
A and B
A= $tformat eq $sformatB= $tformat eq "XML"
ABresult
0X0
100
111
608 0 return $ad->{parser}->export($ad->{storage},$tfile,$tflags);
609 }
610 2 return adConvert('adHash',$ad,$tformat,$tfile,undef,$tflags);
611 }
612 2 ▶sub adConvert {
Subroutine adConvert: called CC=29 SCAR=46.4
Pod documented
613 2 my( $source_format, $source_data,
614 $target_format,$target_file_name,
615 $source_flags,$target_flags )=@_;
616  
617 2 my $target_type = 'STRING';
618 2 ▶ $target_type = 'FILE' if defined $target_file_name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $target_file_name 1 1 2
619 2 ▶ $target_type = 'ARRAY' if $target_format eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $target_format eq "ARRAY" 0 2 2
620  
621 2 my $data_type = 'AD-OBJECT';
622 2 ▶ $data_type = 'ARRAY' if ref $source_data eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
if ref $source_data eq "ARRAY" and ref $source_data->[0] eq "ARRAY" 0 2 2
Condition33%1/3
ref $source_data eq "ARRAY" and ref $source_data->[0] eq "ARRAY"
!ll&&!rl&&r
200
MC/DC0%0/2
ref $source_data eq "ARRAY" and ref $source_data->[0] eq "ARRAY"
ref $source_data eq "ARRAY" ref $source_data->[0] eq "ARRAY"
Truth table33%1/3
A and B
A= ref $source_data eq "ARRAY"B= ref $source_data->[0] eq "ARRAY"
ABresult
0X0
100
111
623 and ref $source_data->[0] eq 'ARRAY';
624  
625 # INIT SOURCE OBJECT
626 2 my $source_ad;
627 2 ▶ if ($source_format eq 'adHash') {
Branch: 1 branch
BranchTrueFalseTotal
if ($source_format eq 'adHash') { } 2 0 2
628 2 $source_ad = $source_data;
629 2 undef $source_data;
630 }
631 else {
632 0 ▶ $source_format = 'CSV' if $source_format =~ /ARRAY/i;
Branch: 1 branch
BranchTrueFalseTotal
if $source_format =~ /ARRAY/i 0 0 0
633 0 $source_ad = adTable(
634 $source_format,$source_data,'r',undef,$source_flags
635 );
636 }
637  
638 # GET COLUMN NAMES
639 2 my @cols;
640 2 ▶ if ( $data_type eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_type eq 'ARRAY') { } 0 2 2
641 0 @cols = @{ shift @{ $source_data } };
642 }
643 else {
644 2 @cols = @{ $source_ad->col_names };
645 }
646  
647  
648 # insert storable here
649 2 ▶ if ('XML HTMLtable' =~ /$target_format/) {
Branch: 1 branch
BranchTrueFalseTotal
if ("XML HTMLtable" =~ /$target_format/) 1 1 2
650 1 $target_flags->{col_names} = join ',',@cols;
651 1 my $target_ad = adTable(
652 $target_format,$target_file_name,'o',undef,$target_flags
653 );
654 1 ▶ if ($data_type eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_type eq "ARRAY") 0 1 1
655 0 for my $row(@$source_data) {
656 0 my @fields=$source_ad->str2ary($row);
657 0 $target_ad->push_row( $source_ad->str2ary(\@fields) );
658 }
659 0 unshift @$source_data, \@cols;
660 0 return $target_ad->export($target_file_name);
661 }
662 1 $source_ad->seek_first_record;
663 1 ▶ while (my $row = $source_ad->get_undeleted_record) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $source_ad->get_undeleted_record) 6 1 7
664 6 $target_ad->push_row( $source_ad->str2ary($row) );
665 }
666 1 return $target_ad->export($target_file_name);
667 }
668  
669 1 my($target_ad,$fh);
670 ### INIT TARGET OBJECT
671 1 ▶ if ($target_type eq 'FILE') {
Branch: 2 branches
BranchTrueFalseTotal
if ($target_type eq 'FILE') { } 0 1 1
elsif ($target_type eq 'STRING') { } 1 0 1
672 0 $target_ad = adTable(
673 $target_format,$target_file_name,'c',undef,$target_flags
674 );
675 0 $fh = $target_ad->{storage}->get_file_handle;
676 }
677 elsif ($target_type eq 'STRING') {
678 1 $target_ad = AnyData->new( $target_format,$target_flags);
679 }
680  
681 1 my($str,$aryref);
682 ### GET COLUMN NAMES
683 1 ▶ if ( !$target_ad->{parser}->{no_col_print} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($target_ad->{"parser"}{"no_col_print"}) 1 0 1
684 1 ▶ if ($target_type eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($target_type eq 'ARRAY') { } 0 1 1
685 0 push @$aryref, \@cols;
686 }
687 else {
688 1 $str = $target_ad->{parser}->write_fields(@cols);
689 1 ▶ $str =~ s/ /,/g if $target_format eq 'Fixed';
Branch: 1 branch
BranchTrueFalseTotal
if $target_format eq "Fixed" 1 0 1
690 1 ▶ if ($target_type eq 'FILE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($target_type eq "FILE") 0 1 1
691 0 $fh->write($str,length $str);
692 }
693 1 ▶ if ($target_type eq 'STRING') {
Branch: 1 branch
BranchTrueFalseTotal
if ($target_type eq "STRING") 1 0 1
694 1 $str = $target_ad->{parser}->write_fields(@cols);
695 }
696 }
697 }
698  
699 # GET DATA
700 1 ▶ if ($data_type eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($data_type eq "ARRAY") 0 1 1
701 0 for my $row(@$source_data) {
702 0 my @fields = $source_ad->str2ary($row);
703 0 my $tmpstr = $target_ad->{parser}->write_fields(@fields);
704 # print $tmpstr if $check;
705 0 ▶ $fh->write($tmpstr,length $tmpstr) if $target_type eq 'FILE';
Branch: 1 branch
BranchTrueFalseTotal
if $target_type eq "FILE" 0 0 0
706 0 ▶ $str .= $tmpstr if $target_type eq 'STRING';
Branch: 1 branch
BranchTrueFalseTotal
if $target_type eq "STRING" 0 0 0
707 }
708 0 unshift @$source_data, \@cols;
709 0 ▶ return $str if $target_format ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $target_format ne "ARRAY" 0 0 0
710 0 return $aryref;
711 }
712 1 $source_ad->seek_first_record; # unless $source_format eq 'XML';
713 1 ▶ while (my $row = $source_ad->get_undeleted_record) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $source_ad->get_undeleted_record) 6 1 7
714 6 ▶ if ($target_format eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($target_format eq "ARRAY") 0 6 6
715 0 ▶ push @$aryref,$row if $target_format eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $target_format eq "ARRAY" 0 0 0
716 0 next;
717 }
718 6 my @fields = $source_ad->str2ary($row);
719 6 my $tmpstr = $target_ad->{parser}->write_fields(@fields);
720 6 ▶ $str .= $target_type eq 'FILE'
Branch: 1 branch
BranchTrueFalseTotal
$target_type eq 'FILE' ? : 0 6 6
721 ? $fh->write($tmpstr,length $tmpstr)
722 : $tmpstr;
723 }
724 1 ▶ return $str if $target_format ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $target_format ne "ARRAY" 1 0 1
725 0 return $aryref;
726 }
727  
728 # if ('Storable' =~ /$target_format/) {
729 # $target_flags->{col_names} = join ',',@cols;
730 # $target_ad = adTable(
731 # $target_format,$target_file_name,'c',undef,$target_flags
732 # );
733 # if (ref $source_data && !$data) {
734 # for my $row(@$source_data) {
735 # push @$data,$row;
736 # }
737 # }
738 # elsif (!$data) {
739 # $source_ad->seek_first_record;
740 # while (my $row = $source_ad->fetch_row) {
741 # push @$data, $row;
742 # }
743 # }
744 # unshift @$data, \@cols;
745 # return $target_ad->{parser}->export($data,$target_file_name);
746 # }
747  
748 12 ▶sub str2ary {
Subroutine str2ary: called CC=2 SCAR=6.9
Pod undocumented
749 12 my($ad,$row) = @_;
750 12 ▶ return @$row if ref $row eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $row eq "ARRAY" 6 6 12
751 6 return $ad->{parser}->read_fields($row);
752 }
753 0 ▶sub ad_string {
Subroutine ad_string: not called CC=1 SCAR=6.9
Pod undocumented
754 0 my($formatref,@fields) = @_;
755 0 my($format,$flags) = split_params($formatref);
756 # &dump($formatref); print "<$format>"; &dump($flags) if $flags;
757 #$formatref =~ s/(.*)/$1/;
758 0 my $ad = AnyData->new( $format, $flags );
759 0 return $ad->{parser}->write_fields(@fields);
760 # return $ad->write_fields(@fields);
761 }
762  
763 0 ▶sub ad_fields {
Subroutine ad_fields: not called CC=1 SCAR=6.9
Pod undocumented
764 0 my($formatref,$str,$flags) = @_;
765 # my($format,$flags) = split_params($formatref);
766 # my $ad = AnyData::new( $format, $flags );
767 0 my $ad = AnyData->new( $formatref, $flags );
768 0 return $ad->{parser}->read_fields($str);
769 }
770  
771 0 ▶sub ad_convert_str {
Subroutine ad_convert_str: not called CC=1 SCAR=6.9
Pod undocumented
772 0 my($source_formatref,$target_formatref,$str) = @_;
773 0 my($source_format,$source_flags) = split_params($source_formatref);
774 0 my($target_format,$target_flags) = split_params($target_formatref);
775 0 my $source_ad = AnyData->new( $source_format,$source_flags);
776 0 my $target_ad = AnyData->new( $target_format,$target_flags);
777 0 my @fields = $source_ad->read_fields($str);
778 0 return $target_ad->write_fields( @fields );
779 }
780  
781 #########################################################
782 # UTILITY METHODS
783 #########################################################
784 #
785 # For all methods that have $format as a parameter,
786 # $format can be either a string name of a format e.g. 'CSV'
787 # or a hashref of the format and flags for that format e.g.
788 # { format => 'FixedWidth', pattern=>'A1 A3 A2' }
789 #
790 # given this parameter, this method returns $format and $flags
791 # setting $flags to {} if none are given
792 #
793 11 ▶sub split_params {
Subroutine split_params: called CC=3 SCAR=13.5
Pod undocumented
794 11 my $source_formatref = shift;
795 11 my $source_flags = {};
796 11 my $source_format = $source_formatref;
797 11 ▶ if (ref $source_formatref eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $source_formatref eq "HASH") 0 11 11
798 0 ▶ while (my($k,$v)=each %$source_formatref) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$source_formatref) 0 0 0
799 0 ($source_format,$source_flags) = ($k,$v);
800 }
801 }
802 #use Data::Dumper;
803 11 return( $source_format, $source_flags);
804 }
805 0 ▶sub dump {
Subroutine dump: not called CC=1 SCAR=6.9
Pod undocumented
806 0 my $var = shift;
807 0 my $name = ref($var);
808 #use Data::Dumper;
809 0 $Data::Dumper::Indent = 1;
810 0 $Data::Dumper::Useqq = 0;
811 0 print Data::Dumper->new([$var],[$name])->Dump();
812 }
813  
814 ###########################################################################
815 # START OF DOCUMENTATION
816 ###########################################################################
817  
818 =pod
819  
820 =head1 NAME
821  
822 AnyData - (DEPRECATED) easy access to data in many formats
823  
824 =head1 SYNOPSIS
825  
826 use AnyData;
827 my $table = adTie( 'CSV','my_db.csv','o', # create a table
828 {col_names=>'name,country,sex'}
829 );
830 $table->{Sue} = {country=>'de',sex=>'f'}; # insert a row
831 delete $table->{Tom}; # delete a single row
832 $str = $table->{Sue}->{country}; # select a single value
833 while ( my $row = each %$table ) { # loop through table
834 print $row->{name} if $row->{sex} eq 'f';
835 }
836 $rows = $table->{{age=>'> 25'}}; # select multiple rows
837 delete $table->{{country=>qr/us|mx|ca/}}; # delete multiple rows
838 $table->{{country=>'Nz'}}={country=>'nz'}; # update multiple rows
839 my $num = adRows( $table, age=>'< 25' ); # count matching rows
840 my @names = adNames( $table ); # get column names
841 my @cars = adColumn( $table, 'cars' ); # group a column
842 my @formats = adFormats(); # list available parsers
843 adExport( $table, $format, $file, $flags ); # save in specified format
844 print adExport( $table, $format, $flags ); # print to screen in format
845 print adDump($table); # dump table to screen
846 undef $table; # close the table
847  
848 #adConvert( $format1, $file1, $format2, $file2 ); # convert btwn formats
849 #print adConvert( $format1, $file1, $format2 ); # convert to screen
850  
851 =head1 DESCRIPTION
852  
853 The rather wacky idea behind this module and its sister module
854 DBD::AnyData is that any data, regardless of source or format should
855 be accessible and modifiable with the same simple set of methods.
856 This module provides a multidimensional tied hash interface to data
857 in a dozen different formats. The DBD::AnyData module adds a DBI/SQL
858 interface for those same formats.
859  
860 Both modules provide built-in protections including appropriate
861 flocking() for all I/O and (in most cases) record-at-a-time access to
862 files rather than slurping of entire files.
863  
864 Currently supported formats include general format flat files (CSV,
865 Fixed Length, etc.), specific formats (passwd files, httpd logs,
866 etc.), and a variety of other kinds of formats (XML, Mp3, HTML
867 tables). The number of supported formats will continue to grow
868 rapidly since there is an open API making it easy for any author to
869 create additional format parsers which can be plugged in to AnyData
870 itself and thereby be accessible by either the tiedhash or DBI/SQL
871 interface.
872  
873 =head1 PREREQUISITES
874  
875 The AnyData.pm module itself is pure Perl and does not depend on
876 anything other than modules that come standard with Perl. Some
877 formats and some advanced features require additional modules: to use
878 the remote ftp/http features, you must have the LWP bundle installed;
879 to use the XML format, you must have XML::Parser and XML::Twig installed;
880 to use the HTMLtable format for reading, you must have HTML::Parser and
881 HTML::TableExtract installed but you can use the HTMLtable for writing
882 with just the standard CGI module. To use DBI/SQL commands, you must have
883 DBI, DBD::AnyData, SQL::Statement and DBD::File installed.
884  
885 =head1 USAGE
886  
887 The AnyData module imports eight methods (functions):
888  
889 =for test ignore
890  
891 adTie() -- create a new table or open an existing table
892 adExport() -- save an existing table in a specified format
893 adConvert() -- convert data in one format into another format
894 adFormats() -- list available formats
895 adNames() -- get the column names of a table
896 adRows() -- get the number of rows in a table or query
897 adDump() -- display the data formatted as an array of rows
898 adColumn() -- group values in a single column
899  
900 The adTie() command returns a special tied hash. The tied hash can
901 then be used to access and/or modify data. See below for details
902  
903 With the exception of the XML, HTMLtable, and ARRAY formats, the
904 adTie() command saves all modifications of the data directly to file
905 as they are made. With XML and HTMLtable, you must make your
906 modifications in memory and then explicitly save them to file with
907 adExport().
908  
909 =head2 adTie()
910  
911 my $table = adTie( $format, $data, $open_mode, $flags );
912  
913 The adTie() command creates a reference to a multidimensional tied hash. In its simplest form, it simply reads a file in a specified format into the tied hash:
914  
915 my $table = adTie( $format, $file );
916  
917 $format is the name of any supported format 'CSV','Fixed','Passwd', etc.
918 $file is the name of a relative or absolute path to a local file
919  
920 e.g.
921 my $table = adTie( 'CSV', '/usr/me/myfile.csv' );
922  
923 this creates a tied hash called $table by reading data in the
924 CSV (comma separated values) format from the file 'myfile.csv'.
925  
926 The hash reference resulting from adTie() can be accessed and modified as follows:
927  
928 use AnyData;
929 my $table = adTie( $format, $file );
930 $table->{$key}->{$column}; # select a value
931 $table->{$key} = {$col1=>$val1,$col2=>$val2...}; # update a row
932 delete $table->{$key}; # delete a row
933 while(my $row = each %$table) { # loop through rows
934 print $row->{$col1} if $row->{$col2} ne 'baz';
935 }
936  
937 The thing returned by adTie ($table in the example) is not an object,
938 it is a reference to a tied hash. This means that hash operations
939 such as exists, values, keys, may be used, keeping in mind that this
940 is a *reference* to a tied hash so the syntax would be
941  
942 for( keys %$table ) {...}
943 for( values %$table ) {...}
944  
945 Also keep in mind that if the table is really large, you probably do
946 not want to use keys and values because they create arrays in memory
947 containing data from every row in the table. Instead use 'each' as
948 shown above since that cycles through the file one record at a time
949 and never puts the entire table into memory.
950  
951 It is also possible to use more advanced searching on the hash, see "Multiple Row Operations" below.
952  
953 In addition to the simple adTie($format,$file), there are other ways to specify additional information in the adTie() command. The full syntax is:
954  
955 my $table = adTie( $format, $data, $open_mode, $flags );
956  
957 The $data parameter allows you to read data from remote files accessible by
958 http or ftp, see "Using Remote Files" below. It also allows you to treat
959 strings and arrays as data sources without needing a file at all, see
960 "Working with Strings and Arrays" below.
961  
962 The optional $mode parameter defaults to 'r' if none is supplied or must be
963 one of
964  
965 'r' read # read only access
966 'u' update # read/write access
967 'c' create # create a new file unless it already exists
968 'o' overwrite # create a new file, overwriting any that already exist
969  
970 The $flags parameter allows you to specify additional information such as column names. See the sections in "Further Details" below.
971  
972 With the exception of the XML, HTMLtable, and ARRAY formats, the
973 adTie() command saves all modifications of the data directly to file
974 as they are made. With XML and HTMLtable, you must make your
975 modifications in memory and then explicitly save them to file with
976 adExport().
977  
978 =head2 adConvert()
979  
980 adConvert( $format1, $data1, $format2, $file2, $flags1, $flags2 );
981  
982 or
983  
984 print adConvert( $format1, $data1, $format2, undef, $flags1, $flags2 );
985  
986 or
987  
988 my $aryref = adConvert( $format1, $data1, 'ARRAY', undef, $flags1 );
989  
990 This method converts data in any supported format into any other supported
991 format. The resulting data may either be saved to a file (if $file2 is
992 supplied as a parameter) or sent back as a string to e.g. print the data
993 to the screen in the new format (if no $file2 is supplied), or sent back
994 as an array reference if $format2 is 'ARRAY'.
995  
996 Some examples:
997  
998 # convert a CSV file into an XML file
999 #
1000 adConvert('CSV','foo.csv','XML','foo.xml');
1001  
1002 # convert a CSV file into an HTML table and print it to the screen
1003 #
1004 print adConvert('CSV','foo.csv','HTMLtable');
1005  
1006 # convert an XML string into a CSV file
1007 #
1008 adConvert('XML', ["<x><motto id='perl'>TIMTOWTDI</motto></x>"],
1009 'CSV','foo.csv'
1010 );
1011  
1012 # convert an array reference into an XML file
1013 #
1014 adConvert('ARRAY', [['id','motto'],['perl','TIMTOWTDI']],
1015 'XML','foo.xml'
1016 );
1017  
1018 # convert an XML file into an array reference
1019 #
1020 my $aryref = adConvert('XML','foo.xml','ARRAY');
1021  
1022 See section below "Using strings and arrays" for details.
1023  
1024 =head2 adExport()
1025  
1026 adExport( $table, $format, $file, $flags );
1027  
1028 or
1029  
1030 print adExport( $table, $format );
1031  
1032 or
1033  
1034 my $aryref = adExport( $table, 'ARRAY' );
1035  
1036 This method converts an existing tied hash into another format and/or
1037 saves the tied hash as a file in the specified format.
1038  
1039 Some examples:
1040  
1041 all assume a previous call to my $table= adTie(...);
1042  
1043 # export table to an XML file
1044 #
1045 adExport($table','XML','foo.xml');
1046  
1047 # export table to an HTML string and print it to the screen
1048 #
1049 print adExport($table,'HTMLtable');
1050  
1051 # export the table to an array reference
1052 #
1053 my $aryref = adExport($table,'ARRAY');
1054  
1055 See section below "Using strings and arrays" for details.
1056  
1057 =head2 adNames()
1058  
1059 my $table = adTie(...);
1060 my @column_names = adNames($table);
1061  
1062 This method returns an array of the column names for the specified table.
1063  
1064 =head2 adRows()
1065  
1066 my $table = adTie(...);
1067 adRows( $table, %search_hash );
1068  
1069 This method takes an AnyData tied hash created with adTie() and
1070 counts the rows in the table that match the search hash.
1071  
1072 For example, this snippet returns a count of the rows in the
1073 file that contain the specified page in the request column
1074  
1075 my $hits = adTie( 'Weblog', 'access.log');
1076 print adRows( $hits , request => 'mypage.html' );
1077  
1078 The search hash may contain multiple search criteria, see the
1079 section on multiple row operations below.
1080  
1081 If the search_hash is omitted, it returns a count of all rows.
1082  
1083 =head2 adColumn()
1084  
1085 my @col_vals = adColumn( $table, $column_name, $distinct_flag );
1086  
1087 This method returns an array of values taken from the specified column.
1088 If there is a distinct_flag parameter, duplicates will be eliminated
1089 from the list.
1090  
1091 For example, this snippet returns a unique list of the values in
1092 the 'player' column of the table.
1093  
1094 my $game = adTie( 'Pipe','games.db' );
1095 my @players = adColumn( $game, 'player', 1 );
1096  
1097 =head2 adDump()
1098  
1099 my $table = adTie(...);
1100 print adDump($table);
1101  
1102 This method prints the raw data in the table. Column names are printed inside angle brackets and separated by colons on the first line, then each row is printed as a list of values inside square brackets.
1103  
1104 =head2 adFormats()
1105  
1106 print "$_\n for adFormats();
1107  
1108 This method shows the available format parsers, e.g. 'CSV', 'XML', etc. It looks in your @INC for the .../AnyData/Format directory and prints the names of format parsing files there. If the parser requires further modules (e.g. XML requires XML::Parser) and you do not have the additional modules installed, the format will not work even if listed by this command. Otherwise, all formats should work as described in this documentation.
1109  
1110 =head1 FURTHER DETAILS
1111  
1112 =head2 Column Names
1113  
1114 Column names may be assigned in three ways:
1115  
1116 * pre -- The format parser preassigns column
1117 names (e.g. Passwd files automatically have
1118 columns named 'username', 'homedir', 'GID', etc.).
1119  
1120 * user -- The user specifies the column names as a comma
1121 separated string associated with the key 'cols':
1122  
1123 my $table = adTie( $format,
1124 $file,
1125 $mode,
1126 {cols=>'name,age,gender'}
1127 );
1128  
1129 * auto -- If there is no preassigned list of column names
1130 and none defined by the user, the first line of
1131 the file is treated as a list of column names;
1132 the line is parsed according to the specific
1133 format (e.g. CSV column names are a comma-separated
1134 list, Tab column names are a tab separated list);
1135  
1136 When creating a new file in a format that does not preassign
1137 column names, the user *must* manually assign them as shown above.
1138  
1139 Some formats have special rules for assigning column names (XML,Fixed,HTMLtable), see the sections below on those formats.
1140  
1141 =head2 Key Columns
1142  
1143 The AnyData modules support tables that have a single key column that
1144 uniquely identifies each row as well as tables that do not have such
1145 keys. For tables where there is a unique key, that key may be assigned
1146 in three ways:
1147  
1148 * pre -- The format parser automatically preassigns the
1149 key column name e.g. Passwd files automatically
1150 have 'username' as the key column.
1151  
1152 * user -- The user specifies the key column name:
1153  
1154 my $table = adTie( $format,
1155 $file,
1156 $mode,
1157 {key=>'country'}
1158 );
1159  
1160 * auto If there is no preassigned key column and the user
1161 does not define one, the first column becomes the
1162 default key column
1163  
1164 =head2 Format Specific Details
1165  
1166 For full details, see the documentation for AnyData::Format::Foo
1167 where Foo is any of the formats listed in the adFormats() command
1168 e.g. 'CSV', 'XML', etc.
1169  
1170 Included below are only some of the more important details of the
1171 specific parsers.
1172  
1173 =over
1174  
1175 =item Fixed Format
1176  
1177 When using the Fixed format for fixed length records you
1178 must always specify a pattern indicating the lengths of the fields.
1179 This should be a string as would be passed to the unpack() function
1180 to unpack the records in your Fixed length definition:
1181  
1182 my $t = adTie( 'Fixed', $file, 'r', {pattern=>'A3 A7 A9'} );
1183  
1184 If you want the column names to appear on the first line of a Fixed
1185 file, they should be in comma-separated format, not in Fixed format.
1186 This is different from other formats which use their own format to
1187 display the column names on the first line. This is necessary because
1188 the name of the column might be longer than the length of the column.
1189  
1190 =item XML Format
1191  
1192 The XML format does not allow you to specify column names as a flag,
1193 rather you specify a "record_tag" and the column names are determined
1194 from the contents of the tag. If no record_tag is specified, the
1195 record tag will be assumed to be the first child of the root of the
1196 XML tree. That child and its structure will be determined from the
1197 DTD if there is one, or from the first occurring record if there is
1198 no DTD.
1199  
1200 For simple XML, no flags are necessary:
1201  
1202 <table>
1203 <row row_id="1"><name>Joe</name><location>Seattle</location></row>
1204 <row row_id="2"><name>Sue</name><location>Portland</location></row>
1205 </table>
1206  
1207 The record_tag will default to the first child, namely "row". The column
1208 names will be generated from the attributes of the record tag and all of
1209 the tags included under the record tag, so the column names in this
1210 example will be "row_id","name","location".
1211  
1212 If the record_tag is not the first child, you will need to specify it. For example:
1213  
1214 <db>
1215 <table table_id="1">
1216 <row row_id="1"><name>Joe</name><location>Seattle</location></row>
1217 <row row_id="2"><name>Sue</name><location>Portland</location></row>
1218 </table>
1219 <table table_id="2">
1220 <row row_id="1"><name>Bob</name><location>Boise</location></row>
1221 <row row_id="2"><name>Bev</name><location>Billings</location></row>
1222 </table>
1223 </db>
1224  
1225 In this case you will need to specify "row" as the record_tag since it is not the first child of the tree. The column names will be generated from the attributes of row's parent (if the parent is not the root), from row's attributes
1226 and sub tags, i.e. "table_id","row_id","name","location".
1227  
1228 When exporting XML, you can specify a DTD to control the output. For example, if you import a table from CSV or from an Array, you can output as XML and specify which of the columns become tags and which become attributes and also specify the nesting of the tags in your DTD.
1229  
1230 The XML format parser is built on top of Michel Rodriguez's excellent XML::Twig which is itself based on XML::Parser. Parameters to either of those modules may be passed in the flags for adTie() and the other commands including the "prettyPrint" flag to specify how the output XML is displayed and things like ProtocolEncoding. ProtocolEncoding defaults to 'ISO-8859-1', all other flags keep the defaults of XML::Twig and XML::Parser. See the documentation of those modules for details;
1231  
1232 CAUTION: Unlike other formats, the XML format does not save changes to
1233 the file as they are entered, but only saves the changes when you explicitly
1234 request them to be saved with the adExport() command.
1235  
1236 =item HTMLtable Format
1237  
1238 This format is based on Matt Sisk's excelletn HTML::TableExtract.
1239  
1240 It can be used to read an existing table from an html page, or to
1241 create a new HTML table from any data source.
1242  
1243 You may control which table in an HTML page is used with the column_names,
1244 depth and count flags.
1245  
1246 If a column_names flag is passed, the first table that contains those names
1247 as the cells in a row will be selected.
1248  
1249 If depth and or count parameters are passed, it will look for tables as
1250 specified in the HTML::TableExtract documentation.
1251  
1252 If none of column_names, depth, or count flags are passed, the first table
1253 encountered in the file will be the table selected and its first row will
1254 be used to determine the column names for the table.
1255  
1256 When exporting to an HTMLtable, you may pass flags to specify properties
1257 of the whole table (table_flags), the top row containing the column names
1258 (top_row_flags), and the data rows (data_row_flags). These flags follow
1259 the syntax of CGI.pm table constructors, e.g.:
1260  
1261 print adExport( $table, 'HTMLtable', {
1262 table_flags => {Border=>3,bgColor=>'blue'};
1263 top_row_flags => {bgColor=>'red'};
1264 data_row_flags => {valign='top'};
1265 });
1266  
1267 The table_flags will default to {Border=>1,bgColor=>'white'} if none
1268 are specified.
1269  
1270 The top_row_flags will default to {bgColor=>'#c0c0c0'} if none are
1271 specified;
1272  
1273 The data_row_flags will be empty if none are specified.
1274  
1275 In other words, if no flags are specified the table will print out with
1276 a border of 1, the column headings in gray, and the data rows in white.
1277  
1278 CAUTION: This module will *not* preserve anything in the html file except
1279 the selected table so if your file contains more than the selected table,
1280 you will want to use adTie() to read the table and then adExport() to write
1281 the table to a different file. When using the HTMLtable format, this is the
1282 only way to preserve changes to the data, the adTie() command will *not*
1283 write to a file.
1284  
1285 =back
1286  
1287 =head2 Multiple Row Operations
1288  
1289 The AnyData hash returned by adTie() may use either single values as keys, or a reference to a hash of comparisons as a key. If the key to the hash is a single value, the hash operates on a single row but if the key to the hash is itself a hash reference, the hash operates on a group of rows.
1290  
1291 my $num_deleted = delete $table->{Sue};
1292  
1293 This example deletes a single row where the key column has the value 'Sue'. If multiple rows have the value 'Sue' in that column, only the first is deleted. It uses a simple string as a key, therefore it operates on only a single row.
1294  
1295 my $num_deleted = delete $table->{ {name=>'Sue'} };
1296  
1297 This example deletes all rows where the column 'name' is equal to 'Sue'. It uses a hashref as a key and therefore operates on multiple rows.
1298  
1299 The hashref used in this example is a single column comparison but the hashref could also include multiple column comparisons. This deletes all rows where the the values listed for the country, gender, and age columns are equal to those specified:
1300  
1301 my $num_deleted = delete $table->{{ country => 'us',
1302 gender => 'm',
1303 age => '25'
1304 }}
1305  
1306  
1307 In addition to simple strings, the values may be specified as regular expressions or as numeric or alphabetic comparisons. This will delete all North American males under the age of 25:
1308  
1309 my $num_deleted = delete $table->{{ country => qr/mx|us|ca/,
1310 gender => 'm',
1311 age => '< 25'
1312 }}
1313  
1314 If numeric or alphabetic comparisons are used, they should be a string with the comparison operator separated from the value by a space, e.g. '> 4' or 'lt b'.
1315  
1316 This kind of search hashref can be used not only to delete multiple rows, but also to update rows. In fact you *must* use a hashref key in order to update your table. Updating is the only operation that can not be done with a single string key.
1317  
1318 The search hashref can be used with a select statement, in which case it returns a reference to an array of rows matching the criteria:
1319  
1320 my $male_players = $table->{{gender=>'m'}};
1321 for my $player( @$male_players ) { print $player->{name},"\n" }
1322  
1323 This should be used with caution with a large table since it gathers all of the selected rows into an array in memory. Again, 'each' is a much better way for large tables. This accomplishes the same thing as the example above, but without ever pulling more than a row into memory at a time:
1324  
1325 while( my $row= each %$table ) {
1326 print $row->{name}, "\n" if $row->{gender}=>'m';
1327 }
1328  
1329 Search criteria for multiple rows can also be used with the adRows() function:
1330  
1331 my $num_of_women = adRows( $table, gender => 'w' );
1332  
1333 That does *not* pull the entire table into memory, it counts the rows a record at a time.
1334  
1335 =head2 Using Remote Files
1336  
1337 If the first file parameter of adTie() or adConvert() begins with "http://" or "ftp://", the file is treated as a remote URL and the LWP module is called behind the scenes to fetch the file. If the files are in an area that requires authentication, that may be supplied in the $flags parameter.
1338  
1339 For example:
1340  
1341 # read a remote file and access it via a tied hash
1342 #
1343 my $table = adTie( 'XML', 'http://www.foo.edu/bar.xml' );
1344  
1345 # same with username/password
1346 #
1347 my $table = ( 'XML', 'ftp://www.foo.edu/pub/bar.xml', 'r'
1348 { user => 'me', pass => 'x7dy4'
1349 );
1350  
1351 # read a remote file, convert it to an HTML table, and print it
1352 #
1353 print adConvert( 'XML', 'ftp://www.foo.edu/pub/bar.xml', 'HTMLtable' );
1354  
1355 =head2 Using Strings and Arrays
1356  
1357 Strings and arrays may be used as either the source of data input or as the target of data output. Strings should be passed as the only element of an array reference (in other words, inside square brackets). Arrays should be a reference to an array whose first element is a reference to an array of column names and whose succeeding elements are references to arrays of row values.
1358  
1359 For example:
1360  
1361 my $table = adTie( 'XML', ["<x><motto id='perl'>TIMTOWTDI</motto></x>"] );
1362  
1363 This uses the XML format to parse the supplied string and returns a tied
1364 hash to the resulting table.
1365  
1366  
1367 my $table = adTie( 'ARRAY', [['id','motto'],['perl','TIMTOWTDI']] );
1368  
1369 This uses the column names "id" and "motto" and the supplied row values
1370 and returns a tied hash to the resulting table.
1371  
1372 It is also possible to use an empty array to create a new empty tied hash in any format, for example:
1373  
1374 my $table = adTie('XML',[],'c');
1375  
1376 creates a new empty tied hash;
1377  
1378 See adConvert() and adExport() for further examples of using strings and arrays.
1379  
1380 =head2 Ties, Flocks, I/O, and Atomicity
1381  
1382 AnyData provides flocking which works under the limitations of flock -- that it only works if other processes accessing the files are also using flock and only on platforms that support flock. See the flock() man page for details.
1383  
1384 Here is what the user supplied open modes actually do:
1385  
1386 r = read only (LOCK_SH) O_RDONLY
1387 u = update (LOCK_EX) O_RDWR
1388 c = create (LOCK_EX) O_CREAT | O_RDWR | O_EXCL
1389 o = overwrite (LOCK_EX) O_CREAT | O_RDWR | O_TRUNC
1390  
1391 When you use something like "my $table = adTie(...)", it opens
1392 the file with a lock and leaves the file and lock open until
1393 1) the hash variable ($table) goes out of scope or 2) the
1394 hash is undefined (e.g. "undef $table") or 3) the hash is
1395 re-assigned to another tie. In all cases the file is closed
1396 and the lock released.
1397  
1398 If adTie is called without creating a tied hash variable, the file
1399 is closed and the lock released immediately after the call to adTie.
1400  
1401 For example: print adTie('XML','foo.xml')->{main_office}->{phone}.
1402  
1403 That obtains a shared lock, opens the file, retrieves the one value
1404 requested, closes the file and releases the lock.
1405  
1406 These two examples accomplish the same thing but the first example
1407 opens the file once, does all of the deletions, keeping the exclusive
1408 lock in place until they are all done, then closes the
1409 file. The second example opens and closes the file three times,
1410 once for each deletion and releases the exclusive lock between each
1411 deletion:
1412  
1413 1. my $t = adTie('Pipe','games.db','u');
1414 delete $t->{"user$_"} for (0..3);
1415 undef $t; # closes file and releases lock
1416  
1417 2. delete adTie('Pipe','games.db','u')->{"user$_"} for (0..3);
1418 # no undef needed since no hash variable created
1419  
1420 =head2 Deletions and Packing
1421  
1422 In order to save time and to prevent having to do writes anywhere except at the end of the file, deletions and updates are *not* done at the time of issuing a delete command. Rather when the user does a delete, the position of the deleted record is stored in a hash and when the file is saved to disk, the deletions are only then physically removed by packing the entire database. Updates are done by inserting the new record at the end of the file and marking the old record for deletion. In the normal course of events, all of this should be transparent and you'll never need to worry about it. However, if your server goes down after you've made updates or deletions but before you've saved the file, then the deleted rows will remain in the database and for updates there will be duplicate rows -- the old non updated row and the new updated row. If you are worried about this kind of event, then use atomic deletes and updates as shown in the section above. There's still a very small possibility of a crash in between the deletion and the save, but in this case it should impact at most a single row. (BIG thanks to Matthew Wickline for suggestions on handling deletes)
1423  
1424 =head1 MORE HELP
1425  
1426 See the README file and the test.pl included with the module
1427 for further examples.
1428  
1429 See the AnyData/Format/*.pm PODs for further details of specific
1430 formats.
1431  
1432 For further support, please use comp.lang.perl.modules
1433  
1434 =head1 ACKNOWLEDGEMENTS
1435  
1436 Special thanks to Andy Duncan, Tom Lowery, Randal Schwartz, Michel Rodriguez, Jochen Wiedmann, Tim Bunce, Alligator Descartes, Mathew Persico, Chris Nandor, Malcom Cook and to many others on the DBI mailing lists and the clp* newsgroups.
1437  
1438 =head1 AUTHOR & COPYRIGHT
1439  
1440 Jeff Zucker <jeff@vpservices.com>
1441  
1442 This module is copyright (c), 2000 by Jeff Zucker.
1443 Some changes (c) 2012 Sven Dowideit L<mailto:SvenDowideit@fosiki.com>
1444 It may be freely distributed under the same terms as Perl itself.
1445  
1446 =cut
1447  
1448 ################################
1449 # END OF AnyData
1450 ################################
1451 6 1;
« AnyData/Storage/RAM.pm ↑ Summary AnyData/Storage/File.pm »