All/Format/Delim.pm

filter: Statementstmt 67.2% 39 / 58 Branchbran 16.6% 2 / 12 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 72.7% 8 / 11 Podpod 0.0% 0 / 5 totaltotal 55.0% 49 / 89 CC 9 SCAR 23.7
CC
9
Cov
57%
contract
30.0
parse2
12.0
expand
3.1
CRAP 10.7
« All/IO/Database.pm ↑ Summary All.pm »
1 package Data::All::Format::Delim;
2  
3  
4 # $Id: Delim.pm,v 1.1.1.1 2005/05/10 23:56:20 dmandelbaum Exp $
5  
6 # TODO: fully implement add_quotes attribute
7  
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use Data::All::Format::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Text::ParseWords qw(quotewords);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use vars qw(@EXPORT $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 @EXPORT = qw();
19 1 $VERSION = 0.10;
20  
21 1 attribute 'delim' => ',';
22 1 attribute 'quote' => '"';
23 1 attribute 'escape' => '\\';
24 1 attribute 'break' => "\n";
25 1 attribute 'add_quotes' => 1;
26  
27 1 attribute 'type';
28  
29 sub expand($);
30 sub contract(\@);
31  
32  
33 sub expand($)
34 # TODO: There are likely better ways to do this. Iterate through
35 # each character? This way is too complex and likely buggy. (slow?)
36 8 ▶{
Subroutine expand: called CC=3 SCAR=11.2
Pod undocumented
37 8 my ($self, $raw) = @_;
38 8 my $record = $raw;
39
40 8 $record =~ s/\"\"(..)\'\'/$1/;
41 # BUG: in Text::Parsewords work around
42 8 ▶ $record =~ s/'/\\'/g if ($raw =~ /'/);
Branch: 1 branch
BranchTrueFalseTotal
if $raw =~ /'/ 0 8 8
43
44 8 my $values = $self->parse(\$record);
45
46 8 ▶ return !wantarray ? $values : @{ $values };
Branch: 1 branch
BranchTrueFalseTotal
!wantarray ? : 0 8 8
47 }
48  
49 sub parse(\$)
50 8 ▶{
Subroutine parse: called CC=1 SCAR=0.0
Pod undocumented
51 8 my ($self, $record) = @_;
52 8 my @values;
53
54 8 my ($d, $q, $e) = ($self->delim, $self->quote, $self->escape);
55
56 8 @values = quotewords($d,0, $$record);
57
58 8 return \@values;
59 }
60  
61 sub parse3(\$)
62 0 ▶{
Subroutine parse3: not called CC=1 SCAR=6.9
Pod undocumented
63 0 my ($self, $record) = @_;
64 0 my @values;
65
66 # my ($d, $q, $e) = ($self->delim, $self->quote, $self->escape);
67
68 #use Regexp::Common qw /delimited/;
69 #while ($$record =~ /$RE{delimited}{-delim=>quotemeta($d)}{-keep}/g)
70 #{
71 # push (@values, $1);
72 #}
73
74
75
76
77 #warn Dumper(\@values);
78 #return \@values;
79 }
80  
81 sub parse2(\$)
82 # A bad solution, CSV only!
83 0 ▶{
Subroutine parse2: not called CC=3 SCAR=24.8
Pod undocumented
84 0 my ($self, $record) = @_;
85 0 my @values;
86
87 0 my ($d, $q, $e) = ($self->delim, $self->quote, $self->escape);
88
89 # From: http://xrl.us/bvci (Experts Exchange)
90 0 ▶ push (@values, $+) while $$record =~ m{
Branch: 1 branch
BranchTrueFalseTotal
$$record =~ / "([^\"\\]*(?:\\.[^\"\\]*)*)",? # groups the phrase inside the quotes | ([^,]+),? | , /gx and do { push @values, $+ } 0 0 0
91 "([^\"\\]*(?:\\.[^\"\\]*)*)",? # groups the phrase inside the quotes
92 | ([^,]+),?
93 | ,
94 }gx;
95
96 0 ▶ push(@values, '') if substr($$record,-1,1) eq $d;
Branch: 1 branch
BranchTrueFalseTotal
if substr($$record, -1, 1) eq $d 0 0 0
97  
98
99 0 return \@values;
100 }
101  
102 sub contract(\@)
103 0 ▶{
Subroutine contract: not called CC=5 SCAR=34.0
Pod undocumented
104 0 my ($self, $values) = @_;
105 0 my @values;
106  
107 0 my $d = $self->delim;
108 0 my $q = $self->quote;
109 0 my $e = $self->escape;
110  
111 0 foreach (@{ $values })
112 {
113 0 ▶ $_ ||= '';
Condition0%0/2
$_ ||= ""
l!l
00
MC/DC0%0/1
$_ ||= ""
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
114
115 0 ▶ $_ =~ s/$q/$e.$q/gx
Branch: 1 branch
BranchTrueFalseTotal
if $q 0 0 0
116 if ($q); # Escape quotes with the values
117
118 0 ▶ ($self->add_quotes())
Branch: 1 branch
BranchTrueFalseTotal
$self->add_quotes ? : 0 0 0
119 ? push(@values, "$q$_$q") # Add quotes...
120 : push(@values, $_); # ...for alphanumeric strings only
121 }
122  
123 0 return CORE::join($d, @values).$self->break;
124 }
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135 1 1;
« All/IO/Database.pm ↑ Summary All.pm »