line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
|
|
1
|
|
882
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
41
|
|
9
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
35
|
|
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
5
|
use base 'Exporter'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
6334
|
|
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
1169
|
use Data::All::Format::Base; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
91
|
|
14
|
1
|
|
|
1
|
|
1789
|
use Text::ParseWords qw(quotewords); |
|
1
|
|
|
|
|
1754
|
|
|
1
|
|
|
|
|
84
|
|
15
|
|
|
|
|
|
|
|
16
|
1
|
|
|
1
|
|
7
|
use vars qw(@EXPORT $VERSION); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
852
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
@EXPORT = qw(); |
19
|
|
|
|
|
|
|
$VERSION = 0.10; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
attribute 'delim' => ','; |
22
|
|
|
|
|
|
|
attribute 'quote' => '"'; |
23
|
|
|
|
|
|
|
attribute 'escape' => '\\'; |
24
|
|
|
|
|
|
|
attribute 'break' => "\n"; |
25
|
|
|
|
|
|
|
attribute 'add_quotes' => 1; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
{ |
37
|
8
|
|
|
8
|
0
|
15
|
my ($self, $raw) = @_; |
38
|
8
|
|
|
|
|
12
|
my $record = $raw; |
39
|
|
|
|
|
|
|
|
40
|
8
|
|
|
|
|
13
|
$record =~ s/\"\"(..)\'\'/$1/; |
41
|
|
|
|
|
|
|
# BUG: in Text::Parsewords work around |
42
|
8
|
50
|
|
|
|
22
|
$record =~ s/'/\\'/g if ($raw =~ /'/); |
43
|
|
|
|
|
|
|
|
44
|
8
|
|
|
|
|
17
|
my $values = $self->parse(\$record); |
45
|
|
|
|
|
|
|
|
46
|
8
|
50
|
|
|
|
23
|
return !wantarray ? $values : @{ $values }; |
|
8
|
|
|
|
|
53
|
|
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub parse(\$) |
50
|
|
|
|
|
|
|
{ |
51
|
8
|
|
|
8
|
0
|
9
|
my ($self, $record) = @_; |
52
|
8
|
|
|
|
|
10
|
my @values; |
53
|
|
|
|
|
|
|
|
54
|
8
|
|
|
|
|
24
|
my ($d, $q, $e) = ($self->delim, $self->quote, $self->escape); |
55
|
|
|
|
|
|
|
|
56
|
8
|
|
|
|
|
30
|
@values = quotewords($d,0, $$record); |
57
|
|
|
|
|
|
|
|
58
|
8
|
|
|
|
|
1647
|
return \@values; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub parse3(\$) |
62
|
|
|
|
|
|
|
{ |
63
|
0
|
|
|
0
|
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
|
|
|
|
|
|
|
{ |
84
|
0
|
|
|
0
|
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{ |
91
|
|
|
|
|
|
|
"([^\"\\]*(?:\\.[^\"\\]*)*)",? # groups the phrase inside the quotes |
92
|
|
|
|
|
|
|
| ([^,]+),? |
93
|
|
|
|
|
|
|
| , |
94
|
|
|
|
|
|
|
}gx; |
95
|
|
|
|
|
|
|
|
96
|
0
|
0
|
|
|
|
|
push(@values, '') if substr($$record,-1,1) eq $d; |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
|
99
|
0
|
|
|
|
|
|
return \@values; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub contract(\@) |
103
|
|
|
|
|
|
|
{ |
104
|
0
|
|
|
0
|
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 }) |
|
0
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
{ |
113
|
0
|
|
0
|
|
|
|
$_ ||= ''; |
114
|
|
|
|
|
|
|
|
115
|
0
|
0
|
|
|
|
|
$_ =~ s/$q/$e.$q/gx |
116
|
|
|
|
|
|
|
if ($q); # Escape quotes with the values |
117
|
|
|
|
|
|
|
|
118
|
0
|
0
|
|
|
|
|
($self->add_quotes()) |
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; |