line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::FixedLength::Extra; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
require 5.005_62; |
5
|
1
|
|
|
1
|
|
659
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
41
|
|
6
|
1
|
|
|
1
|
|
7
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
32
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
922
|
use Text::FixedLength; |
|
1
|
|
|
|
|
1307
|
|
|
1
|
|
|
|
|
920
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
require Exporter; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# Items to export into callers namespace by default. Note: do not export |
15
|
|
|
|
|
|
|
# names by default without a very good reason. Use EXPORT_OK instead. |
16
|
|
|
|
|
|
|
# Do not simply export all your public functions/methods/constants. |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# This allows declaration use Text::FixedLength::Extra ':all'; |
19
|
|
|
|
|
|
|
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK |
20
|
|
|
|
|
|
|
# will save memory. |
21
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( 'all' => [ qw( |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
) ] ); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
our @EXPORT = qw(fixedlength |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
); |
30
|
|
|
|
|
|
|
our $VERSION = '1.1'; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
our $debug = 0; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# Preloaded methods go here. |
36
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------- |
37
|
|
|
|
|
|
|
# Subroutine: getFixed - given a string, delimiter, and format return a string |
38
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------- |
39
|
|
|
|
|
|
|
sub Text::FixedLength::getFixed { |
40
|
0
|
|
0
|
0
|
0
|
|
my $s = shift || die 'getFixed: need a string'; |
41
|
0
|
|
0
|
|
|
|
my $delim = shift || die 'getFixed: need a delimiter'; |
42
|
0
|
|
0
|
|
|
|
my $format = shift || die 'getFixed: need a format'; |
43
|
0
|
|
|
|
|
|
my $out = ''; |
44
|
0
|
0
|
|
|
|
|
die "getFixed: no delimiter in $s" unless $s =~ /$delim/; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# -- get each piece |
47
|
0
|
|
|
|
|
|
my @records = split /$delim/, $s; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# -- setup the sprintf format (e.g. "%-8s%3s...") |
50
|
0
|
|
|
|
|
|
my $count = 0; |
51
|
0
|
|
|
|
|
|
foreach my $format ( @$format ) { |
52
|
|
|
|
|
|
|
sub assign_just { |
53
|
0
|
0
|
|
0
|
0
|
|
$_[0] eq 'L' ? '-' : ''; |
54
|
|
|
|
|
|
|
} |
55
|
0
|
|
|
|
|
|
my $just = assign_just $Text::FixedLength::defaultJustification; |
56
|
0
|
|
|
|
|
|
my ($width,$d_or_f,$zero_fill,$decimal_places,$numfmt); |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
|
59
|
0
|
|
|
|
|
|
my $int_re = '([*])?(D)'; |
60
|
0
|
|
|
|
|
|
my $flt_re = '([*])?(F)(\d+)?'; |
61
|
0
|
|
|
|
|
|
my $numfmt_re = "($int_re|$flt_re)"; |
62
|
0
|
|
|
|
|
|
my $format_re =<
|
63
|
|
|
|
|
|
|
(\\d+) # width |
64
|
|
|
|
|
|
|
(R|L)? # optional justification |
65
|
|
|
|
|
|
|
( # optional numerical formatting |
66
|
|
|
|
|
|
|
$numfmt_re |
67
|
|
|
|
|
|
|
)? |
68
|
|
|
|
|
|
|
RE |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
# ---- |
71
|
|
|
|
|
|
|
|
72
|
0
|
0
|
|
|
|
|
if ($format =~ /$format_re/x) { |
73
|
|
|
|
|
|
|
|
74
|
0
|
|
|
|
|
|
$width=$1; |
75
|
0
|
0
|
|
|
|
|
if ($2) { $just = assign_just $2 } |
|
0
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
|
77
|
0
|
0
|
|
|
|
|
warn "$3 =~ /$numfmt_re/" if $debug; |
78
|
0
|
|
|
|
|
|
my $text = $3; |
79
|
0
|
0
|
0
|
|
|
|
if ($text =~ /$int_re/i or $text =~ /$flt_re/) { |
80
|
0
|
0
|
|
|
|
|
warn "RE:$1.$2.$3.$4" if $debug; |
81
|
0
|
0
|
|
|
|
|
$zero_fill = '0' if ($1); |
82
|
0
|
|
|
|
|
|
$d_or_f = lc $2; |
83
|
0
|
0
|
|
|
|
|
warn "d_of_f: $d_or_f" if $debug; |
84
|
0
|
0
|
|
|
|
|
$d_or_f = ".$3$d_or_f" if ($d_or_f eq 'f'); |
85
|
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
|
my $new_out = "%${just}${zero_fill}${width}${d_or_f}"; |
87
|
0
|
0
|
|
|
|
|
warn "num sprintf :$new_out" if $debug; |
88
|
0
|
|
|
|
|
|
$out .= $new_out; |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
} else { |
91
|
0
|
|
|
|
|
|
my $new_out = "%${just}${width}s"; |
92
|
0
|
0
|
|
|
|
|
warn "str sprintf: $new_out" if $debug; |
93
|
0
|
|
|
|
|
|
$out .= $new_out; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
} else { |
97
|
0
|
|
|
|
|
|
die "$format did not match $format_re"; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
# -- Crop the record if it is longer than it is meant to be |
100
|
0
|
0
|
|
|
|
|
if ($Text::FixedLength::cropRecords) { |
101
|
0
|
0
|
|
|
|
|
$records[$count] = substr($records[$count], 0, $width) |
102
|
|
|
|
|
|
|
if length $records[$count] > $width; |
103
|
|
|
|
|
|
|
} |
104
|
0
|
|
|
|
|
|
$count++; |
105
|
|
|
|
|
|
|
} |
106
|
0
|
0
|
|
|
|
|
warn "sprintf stmt: $out" if $debug; |
107
|
0
|
|
|
|
|
|
return sprintf $out, @records; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub fixedlength { |
112
|
0
|
|
|
0
|
0
|
|
my ($format_href, $data_href, $field_order_ref) = @_; |
113
|
|
|
|
|
|
|
|
114
|
0
|
|
|
|
|
|
my $delim = "\t"; |
115
|
0
|
|
|
|
|
|
my (@format,@data); |
116
|
|
|
|
|
|
|
|
117
|
0
|
|
|
|
|
|
for (@$field_order_ref) { |
118
|
0
|
|
|
|
|
|
push @format, $format_href->{$_}; |
119
|
0
|
|
|
|
|
|
push @data, $data_href->{$_}; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
0
|
|
|
|
|
|
my $data = join $delim, @data; |
123
|
|
|
|
|
|
|
|
124
|
0
|
|
|
|
|
|
[ delim2fixed([$data], $delim, \@format) ] -> [0]; |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
1; |
128
|
|
|
|
|
|
|
__END__ |