| 1 | package Geo::GeoNames::File; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | Geo::GeoNames::File - Perl module for handling GeoNames.org data files | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | use Geo::GeoNames::File; | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | my $file = Geo::GeoNames::File->open( qw/US.txt GB.txt/ ); | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | while( my $rec = $file->next() ) | ||||||||||||||||||||||||||||
| 14 | { | ||||||||||||||||||||||||||||
| 15 | print $rec->name . "\n"; | ||||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | $file->close(); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | Provides a Perl extention for handling GeoNames.org data files. You may | ||||||||||||||||||||||||||||
| 23 | use this module to load GeoNames.org records from several seperate files. | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | Xiangrui Meng <mengxr@stanford.edu> | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | Copyright (C) 2009 by Xiangrui Meng | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 34 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||||||||||||||||||||||||||||
| 35 | at your option, any later version of Perl 5 you may have available. | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =cut | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 2 | ▶ | use 5.008007; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 2 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 2 | require Geo::GeoNames::Record; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =over | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =item open() | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | Open files. | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | my $file = Geo::GeoNames::File->open( @geonames_filenames ); | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | =cut | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | sub open | ||||||||||||||||||||||||||||
| 58 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
open: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | 2 | my $class = shift; | |||||||||||||||||||||||||||
| 60 | 2 | my @filenames = @_; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 2 | my $self = { | |||||||||||||||||||||||||||
| 63 | filenames => \@filenames, | ||||||||||||||||||||||||||||
| 64 | cur_filename => undef, | ||||||||||||||||||||||||||||
| 65 | cur_fh => undef, | ||||||||||||||||||||||||||||
| 66 | }; | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 2 | bless $self, $class; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 2 | return $self; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =item close() | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | Close open file handles. | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | =cut | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | sub close | ||||||||||||||||||||||||||||
| 80 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
close: called CC=2 SCAR=8.1
Pod
documented
| |||||||||||||||||||||||||||||
| 81 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 2 | ▶ | if( $self->{cur_fh} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | { | ||||||||||||||||||||||||||||
| 85 | 0 | CORE::close $self->{cur_fh}; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =item next() | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | my $record = $batch->next(); | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | Return the next record in the file as a Geo::GeoNames::Record object. If a | ||||||||||||||||||||||||||||
| 94 | filter function is supplied, it will return the next filtered record. | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | sub pop_gt_100000 | ||||||||||||||||||||||||||||
| 97 | { | ||||||||||||||||||||||||||||
| 98 | return (shift->population > 100000); | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | my $record = $file->next( \&pop_gt_100000 ); | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =cut | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | sub next | ||||||||||||||||||||||||||||
| 106 | 20 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
next: called CC=10 SCAR=23.2
Pod
documented
| |||||||||||||||||||||||||||||
| 107 | 20 | my ( $self, $filter ) = @_; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 20 | ▶ | if ( $filter and ref($filter) ne 'CODE' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$filter and ref $filter ne "CODE"
MC/DC0%0/2
$filter and ref $filter ne "CODE"
$filter
ref $filter ne "CODE"
Truth table66%2/3
A and B
A= $filterB= ref $filter ne "CODE"
| |||||||||||||||||||||||||||||
| 110 | { | ||||||||||||||||||||||||||||
| 111 | 0 | Carp::croak( "filter function must be a subroutine reference." ); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 20 | ▶ | if( $self->{cur_fh} ) # if cur_fh is active | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | { | ||||||||||||||||||||||||||||
| 116 | 15 | while(1) | |||||||||||||||||||||||||||
| 117 | { | ||||||||||||||||||||||||||||
| 118 | 33 | ▶ | if( eof($self->{cur_fh}) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | { | ||||||||||||||||||||||||||||
| 120 | 3 | CORE::close $self->{cur_fh}; | |||||||||||||||||||||||||||
| 121 | 3 | $self->{cur_fh} = undef; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 3 | return $self->next($filter); | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | else | ||||||||||||||||||||||||||||
| 126 | { | ||||||||||||||||||||||||||||
| 127 | 30 | my $fh = $self->{cur_fh}; | |||||||||||||||||||||||||||
| 128 | 30 | my $line = <$fh>; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 30 | ▶ | next if $line =~ /^\s*#/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 30 | my $rec = Geo::GeoNames::Record->new( $line ); | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 30 | ▶ | next if( $filter and !($filter->($rec)) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$filter and not &$filter($rec)
MC/DC100%2/2
$filter and not &$filter($rec)
$filter
not &$filter($rec)
Truth table100%3/3
A and not(B)
A= $filterB= not &$filter($rec)
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 12 | return $rec; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | else # open next file | ||||||||||||||||||||||||||||
| 141 | { | ||||||||||||||||||||||||||||
| 142 | 5 | ▶ | $self->{cur_filename} = shift @{$self->{filenames}} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | or return; | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | CORE::open( $self->{cur_fh}, "<:utf8", $self->{cur_filename} ) | ||||||||||||||||||||||||||||
| 146 | 3 | ▶ | or Carp::croak( "Couldn't open $self->{cur_filename}!" ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 3 | return $self->next($filter); | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =back | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =cut | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 2 | 1; | |||||||||||||||||||||||||||
| 157 | __END__ | ||||||||||||||||||||||||||||