| 1 | package Geo::TigerLine::Record::Parser; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use Carp::Assert; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use vars qw($VERSION); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 1 | $VERSION = '0.01'; | |||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | =pod | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | Geo::TigerLine::Record::Parser - Parsing superclass for TIGER/Line records. | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | package Geo::TigerLine::Record::23; | ||||||||||||||||||||
| 17 | use base qw(Geo::TigerLine::Record::Parser); | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | @records = __PACKAGE__->parse_file($fh); | ||||||||||||||||||||
| 20 | __PACKAGE__->parse_file($fh, \&callback); | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | $record = __PACKAGE__->parse($row); | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | =head1 DESCRIPTION | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | Parses raw TIGER/Line data into Geo::TigerLine::Record objects. This | ||||||||||||||||||||
| 28 | is intended to be used as a superclass of Geo::TigerLine::Record | ||||||||||||||||||||
| 29 | objects and not used directly. | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | You shouldn't be here. | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | =head2 Methods | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | =over 4 | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | =item B<parse_file> | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | @records = __PACKAGE__->parse_file($fh); | ||||||||||||||||||||
| 41 | __PACKAGE__->parse_file($fh, \&callback); | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | Parses a given filehandle as a TIGER/Line data file. The data | ||||||||||||||||||||
| 44 | definition is taken from __PACKAGE__->Pack_Tmpl, __PACKAGE__->Dict and | ||||||||||||||||||||
| 45 | __PACKAGE__->Fields. Returns an array of objects of type __PACKAGE__. | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | &callback will be called for each record and given a record object and | ||||||||||||||||||||
| 48 | its position in the file (ie. 1 for the first, 2 for the second, etc...). | ||||||||||||||||||||
| 49 | A sample callback... | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | sub callback { | ||||||||||||||||||||
| 52 | my($record, $pos) = @_; | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | printf "Record #$pos is %s\n", $record->tlid; | ||||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | If a &callback is given, a list of records will B<NOT> be returned. | ||||||||||||||||||||
| 58 | It is assumed you'll be taking care of arrangements to store the | ||||||||||||||||||||
| 59 | records in your callback and @records can eat up huge amounds of | ||||||||||||||||||||
| 60 | memory for a typical TIGER/Line data file. | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | =cut | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | #'# | ||||||||||||||||||||
| 65 | 2 | ▶ | sub parse_file { | ||||||||||||||||||
|
Subroutine
parse_file: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||
| 66 | 2 | my($proto, $fh, $callback) = @_; | |||||||||||||||||||
| 67 | 2 | ▶ | my($class) = ref $proto || $proto; | ||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 2 | my @records = (); | |||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 2 | my $num = 1; | |||||||||||||||||||
| 72 | 2 | ▶ | while(<$fh>) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 73 | 12 | chomp; | |||||||||||||||||||
| 74 | 12 | my $record = $class->parse($_); | |||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 12 | ▶ | if( defined $callback ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | 6 | $callback->($record, $num); | |||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | else { | ||||||||||||||||||||
| 80 | 6 | push @records, $record; | |||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 12 | $num++; | |||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 2 | return @records; | |||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | =pod | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | =item B<parse> | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | $record = __PACKAGE__->parse($line); | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | Parses a single record of TIGER/Line data. | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | =cut | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 12 | ▶ | sub parse { | ||||||||||||||||||
|
Subroutine
parse: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 100 | 12 | my($proto, $line) = @_; | |||||||||||||||||||
| 101 | 12 | ▶ | my($class) = ref $proto || $proto; | ||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 12 | my $data_def = $class->Dict; | |||||||||||||||||||
| 104 | 12 | my $data_fields = $class->Fields; | |||||||||||||||||||
| 105 | 12 | my @fields = unpack($class->Pack_Tmpl, $line); | |||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 12 | assert(@fields == keys %$data_def); | |||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 12 | my %fields = map { ($_ => shift @fields) } @$data_fields; | |||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | # Clip leading whitespace off right justified fields. | ||||||||||||||||||||
| 112 | 12 | foreach my $field ( map { $_->{field} } grep { $_->{fmt} eq 'R' } | |||||||||||||||||||
| 113 | values %$data_def ) | ||||||||||||||||||||
| 114 | { | ||||||||||||||||||||
| 115 | 120 | $fields{$field} =~ s/^\s+//; | |||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 12 | my $obj = $class->new(\%fields); | |||||||||||||||||||
| 119 | 12 | return $obj; | |||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | =pod | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | =back | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | =head1 AUTHOR | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | Michael G Schwern <schwern@pobox.com> | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | =head1 SEE ALSO | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | L<Geo::TigerLine> | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | =cut | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | 1 | 1; | |||||||||||||||||||
| 137 | |||||||||||||||||||||