File.pm

filter: Statementstmt 95.0% 38 / 40 Branchbran 75.0% 12 / 16 Conditioncond 83.3% 5 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 7 / 7 Podpod 100% 3 / 3 totaltotal 88.1% 67 / 76 CC 11 SCAR 18.8
CC
11
Cov
89%
next
10.2
close
2.3
BEGIN
1.0
CRAP 6.6
↑ Summary Record.pm »
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
BranchTrueFalseTotal
if ($self->{"cur_fh"}) 0 2 2
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
BranchTrueFalseTotal
if ($filter and ref $filter ne "CODE") 0 20 20
Condition66%2/3
$filter and ref $filter ne "CODE"
!ll&&!rl&&r
1370
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"
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if ($self->{'cur_fh'}) { } 15 5 20
115 {
116 15 while(1)
117 {
118 33 ▶ if( eof($self->{cur_fh}) )
Branch: 1 branch
BranchTrueFalseTotal
if (eof $self->{'cur_fh'}) { } 3 30 33
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
BranchTrueFalseTotal
if $line =~ /^\s*#/ 0 30 30
131  
132 30 my $rec = Geo::GeoNames::Record->new( $line );
133  
134 30 ▶ next if( $filter and !($filter->($rec)) );
Branch: 1 branch
BranchTrueFalseTotal
if $filter and not &$filter($rec) 18 12 30
Condition100%3/3
$filter and not &$filter($rec)
!ll&&!rl&&r
10218
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)
ABresult
0X0
100
111
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
BranchTrueFalseTotal
$self->{"cur_filename"} = shift @{$$self{"filenames"};} or return 3 2 5
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
BranchTrueFalseTotal
CORE::open $self->{"cur_fh"}, "<:utf8", $self->{"cur_filename"} or Carp::croak(qq[Couldn't open $self->{"cur_filename"}!]) 3 0 3
147
148 3 return $self->next($filter);
149 }
150 }
151  
152 =back
153  
154 =cut
155  
156 2 1;
157 __END__
↑ Summary Record.pm »