| 1 | package Text::FixedLength::Extra; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 1 | require 5.005_62; | |||||||||||||||||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Text::FixedLength; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | require Exporter; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | 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 | 1 | our %EXPORT_TAGS = ( 'all' => [ qw( | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | ) ] ); | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 1 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | our @EXPORT = qw(fixedlength | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||||||||||
| 30 | 1 | our $VERSION = '1.1'; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 1 | 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 | 0 | ▶ | sub Text::FixedLength::getFixed { | ||||||||||||||||||||||||||
|
Subroutine
getFixed: not called CC=20 SCAR=60.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | my $s = shift || die 'getFixed: need a string'; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || die('getFixed: need a string')
MC/DC0%0/1
shift() || die('getFixed: need a string')
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 41 | 0 | ▶ | my $delim = shift || die 'getFixed: need a delimiter'; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || die('getFixed: need a delimiter')
MC/DC0%0/1
shift() || die('getFixed: need a delimiter')
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 42 | 0 | ▶ | my $format = shift || die 'getFixed: need a format'; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || die('getFixed: need a format')
MC/DC0%0/1
shift() || die('getFixed: need a format')
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 43 | 0 | my $out = ''; | |||||||||||||||||||||||||||
| 44 | 0 | ▶ | die "getFixed: no delimiter in $s" unless $s =~ /$delim/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 0 | ▶ | sub assign_just { | ||||||||||||||||||||||||||
|
Subroutine
assign_just: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | $_[0] eq 'L' ? '-' : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 =<<RE; | |||||||||||||||||||||||||||
| 63 | (\\d+) # width | ||||||||||||||||||||||||||||
| 64 | (R|L)? # optional justification | ||||||||||||||||||||||||||||
| 65 | ( # optional numerical formatting | ||||||||||||||||||||||||||||
| 66 | $numfmt_re | ||||||||||||||||||||||||||||
| 67 | )? | ||||||||||||||||||||||||||||
| 68 | RE | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # ---- | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | if ($format =~ /$format_re/x) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | $width=$1; | |||||||||||||||||||||||||||
| 75 | 0 | ▶ | if ($2) { $just = assign_just $2 } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | warn "$3 =~ /$numfmt_re/" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 0 | my $text = $3; | |||||||||||||||||||||||||||
| 79 | 0 | ▶ | if ($text =~ /$int_re/i or $text =~ /$flt_re/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$text =~ /$int_re/i or $text =~ /$flt_re/
MC/DC0%0/2
$text =~ /$int_re/i or $text =~ /$flt_re/
$text =~ /$int_re/i
$text =~ /$flt_re/
Truth table0%0/3
A or B
A= $text =~ /$int_re/iB= $text =~ /$flt_re/
| |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | warn "RE:$1.$2.$3.$4" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | $zero_fill = '0' if ($1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | $d_or_f = lc $2; | |||||||||||||||||||||||||||
| 83 | 0 | ▶ | warn "d_of_f: $d_or_f" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | $d_or_f = ".$3$d_or_f" if ($d_or_f eq 'f'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | my $new_out = "%${just}${zero_fill}${width}${d_or_f}"; | |||||||||||||||||||||||||||
| 87 | 0 | ▶ | warn "num sprintf :$new_out" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | $out .= $new_out; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | } else { | ||||||||||||||||||||||||||||
| 91 | 0 | my $new_out = "%${just}${width}s"; | |||||||||||||||||||||||||||
| 92 | 0 | ▶ | warn "str sprintf: $new_out" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ▶ | if ($Text::FixedLength::cropRecords) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | $records[$count] = substr($records[$count], 0, $width) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | if length $records[$count] > $width; | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | 0 | $count++; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | 0 | ▶ | warn "sprintf stmt: $out" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 0 | return sprintf $out, @records; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | sub fixedlength { | ||||||||||||||||||||||||||
|
Subroutine
fixedlength: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 112 | 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 | 1; | |||||||||||||||||||||||||||
| 128 | __END__ | ||||||||||||||||||||||||||||