| 1 | ######################################################### | ||||||||||||
| 2 | package AnyData::Format::Fixed; | ||||||||||||
| 3 | ######################################################### | ||||||||||||
| 4 | # copyright (c) 2000, Jeff Zucker <jeff@vpservices.com> | ||||||||||||
| 5 | ######################################################### | ||||||||||||
| 6 | |||||||||||||
| 7 | =head1 NAME | ||||||||||||
| 8 | |||||||||||||
| 9 | AnyData::Format::Fixed - tiedhash & DBI/SQL access to Fixed length data | ||||||||||||
| 10 | |||||||||||||
| 11 | =head1 SYNOPSIS | ||||||||||||
| 12 | |||||||||||||
| 13 | use AnyData; | ||||||||||||
| 14 | my $table = adHash( 'Fixed', $filename,'r',{pattern=>'A20 A2'} ); | ||||||||||||
| 15 | while (my $row = each %$table) { | ||||||||||||
| 16 | print $row->{name},"\n" if $row->{country} =~ /us|mx|ca/; | ||||||||||||
| 17 | } | ||||||||||||
| 18 | # ... other tied hash operations | ||||||||||||
| 19 | |||||||||||||
| 20 | OR | ||||||||||||
| 21 | |||||||||||||
| 22 | use DBI | ||||||||||||
| 23 | my $dbh = DBI->connect('dbi:AnyData:'); | ||||||||||||
| 24 | $dbh->func('table1','Fixed', $filename, {pattern=>'A20 A2'},'ad_catalog'); | ||||||||||||
| 25 | my $hits = $dbh->selectall_arrayref( qq{ | ||||||||||||
| 26 | SELECT name FROM table1 WHERE country = 'us' | ||||||||||||
| 27 | }); | ||||||||||||
| 28 | # ... other DBI/SQL operations | ||||||||||||
| 29 | |||||||||||||
| 30 | =head1 DESCRIPTION | ||||||||||||
| 31 | |||||||||||||
| 32 | This is a parser for fixed length record files. You must specify an unpack pattern listing the widths of the fields e.g. {pattern=>'A3 A7 A20'}. You can either supply the column names or let the module get them for you from the first line of the file. In either case, they should be a comma separated string. | ||||||||||||
| 33 | |||||||||||||
| 34 | Refer to L<http://perldoc.perl.org/functions/pack.html> for the formatting of the pattern. | ||||||||||||
| 35 | |||||||||||||
| 36 | Please refer to the documentation for AnyData.pm and DBD::AnyData.pm | ||||||||||||
| 37 | for further details. | ||||||||||||
| 38 | |||||||||||||
| 39 | =head1 AUTHOR & COPYRIGHT | ||||||||||||
| 40 | |||||||||||||
| 41 | copyright 2000, Jeff Zucker <jeff@vpservices.com> | ||||||||||||
| 42 | all rights reserved | ||||||||||||
| 43 | |||||||||||||
| 44 | =cut | ||||||||||||
| 45 | |||||||||||||
| 46 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 47 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 48 | 2 | ▶ | use AnyData::Format::Base; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 49 | 2 | ▶ | use vars qw( @ISA $VERSION); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 50 | 2 | @AnyData::Format::Fixed::ISA = qw( AnyData::Format::Base ); | |||||||||||
| 51 | |||||||||||||
| 52 | 2 | $VERSION = '0.12'; | |||||||||||
| 53 | |||||||||||||
| 54 | 34 | ▶ | sub read_fields { | ||||||||||
|
Subroutine
read_fields: called CC=3 SCAR=14.2
Pod
undocumented
| |||||||||||||
| 55 | 34 | my $self = shift; | |||||||||||
| 56 | 34 | my $str = shift; | |||||||||||
| 57 | 34 | ▶ | if (!$self->{pattern}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 58 | 0 | print "NO UNPACK PATTERN SPECIFIED!"; exit; | |||||||||||
| 59 | } | ||||||||||||
| 60 | 34 | my @fields = unpack $self->{pattern}, $str; | |||||||||||
| 61 | 34 | ▶ | if ($self->{trim}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 62 | 0 | @fields = map {s/^\s+//; s/\s+$//; $_} @fields; | |||||||||||
| 63 | } | ||||||||||||
| 64 | 34 | return @fields; | |||||||||||
| 65 | } | ||||||||||||
| 66 | |||||||||||||
| 67 | 14 | ▶ | sub write_fields { | ||||||||||
|
Subroutine
write_fields: called CC=8 SCAR=21.0
Pod
undocumented
| |||||||||||||
| 68 | 14 | my $self = shift; | |||||||||||
| 69 | 14 | my @fields = @_; | |||||||||||
| 70 | 14 | my $fieldNum =0; | |||||||||||
| 71 | 14 | ▶ | my $patternStr = $self->{pattern} || ''; | ||||||||||
|
Condition50%1/2
$self->{'pattern'} || ''
MC/DC0%0/1
$self->{'pattern'} || ''
$self->{'pattern'}
Truth table50%1/2
A
A= $self->{'pattern'}
| |||||||||||||
| 72 | 14 | $patternStr =~ s/[a-zA-Z]//gi; | |||||||||||
| 73 | 14 | my @fieldLengths = split /\s+/, $patternStr; | |||||||||||
| 74 | 14 | my $fieldStr = ''; | |||||||||||
| 75 | 14 | for(@fields) { | |||||||||||
| 76 | 34 | ▶ | next unless defined $_; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 77 | # PAD OR TRUNCATE DATA TO FIT WITHIN FIELD LENGTHS | ||||||||||||
| 78 | 34 | ▶ | my $oldLen = length $_ || 0; | ||||||||||
|
Condition50%1/2
length $_ || 0
MC/DC0%0/1
length $_ || 0
length $_
Truth table50%1/2
A
A= length $_
| |||||||||||||
| 79 | 34 | ▶ | my $newLen = $fieldLengths[$fieldNum] || 0; | ||||||||||
|
Condition50%1/2
$fieldLengths[$fieldNum] || 0
MC/DC0%0/1
$fieldLengths[$fieldNum] || 0
$fieldLengths[$fieldNum]
Truth table50%1/2
A
A= $fieldLengths[$fieldNum]
| |||||||||||||
| 80 | 34 | ▶ | if ($oldLen < $newLen) { $_ = sprintf "%-${newLen}s",$_; } | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 81 | 34 | ▶ | if ($oldLen > $newLen) { $_ = substr $_, 0, $newLen; } | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 82 | 34 | $fieldNum++; | |||||||||||
| 83 | 34 | $fieldStr .= $_; | |||||||||||
| 84 | } | ||||||||||||
| 85 | 14 | $fieldStr .= $self->{record_sep}; | |||||||||||
| 86 | #print "<$fieldStr>"; | ||||||||||||
| 87 | 14 | return $fieldStr; | |||||||||||
| 88 | } | ||||||||||||
| 89 | 2 | 1; | |||||||||||
| 90 | |||||||||||||
| 91 | |||||||||||||
| 92 | |||||||||||||
| 93 | |||||||||||||
| 94 | |||||||||||||