script/gaccleanupscript.pl untested

filter: Statementstmt 0.0% 0 / 65 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 3 Podpod n/a totaltotal 0.0% 0 / 84 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/local/bin/perl
2  
3 =head2 NAME
4  
5 Geographic Area Code List Cleanup
6  
7 =head2 DESCRIPTION
8  
9 Reads MARC code list for geographic area codes (plain text).
10 Writes only codes and country (or other place) name, separated by tab.
11 For obsolete codes, adds these at end of output file (keeping the hyphen).
12  
13 =head2 TO DO
14  
15 Rewrite code to account for split lines (language name too long to fit on one line, so it is split onto another, possibly during save from Web browser.
16 Easier to take care of these exceptions manually, as there are only a few and the code list shouldn't need to be changed very often.
17  
18 =cut
19  
20 ###########################
21 ### Initialize includes ###
22 ### and basic needs ###
23 ###########################
24 use strict;
25 #use Sort::Fields;
26 use MARC::BBMARC;
27 ##########################
28 ## Time coding routines ##
29 ## Print start time and ##
30 ## set start variable ##
31 ##########################
32  
33 use Time::HiRes qw( tv_interval );
34 # measure elapsed time
35 my $t0 = [Time::HiRes::time()];
36 my $startingtime = MARC::BBMARC::startstop_time();
37 #########################
38 ### Start main program ##
39 #########################
40  
41 print ("Welcome to Geographic Area Code List Cleanup\n");
42  
43 ##### File handling initialization ######
44 #prompt for input file
45 print ("What is the input file? ");
46 my $inputfile=<>;
47 chomp $inputfile;
48 $inputfile =~ s/^\"(.*)\"$/$1/;
49 open (IN, "<$inputfile") or die "Can not open $inputfile";
50 print ("What is the export file? ");
51 my $exportfile = <>;
52 chomp $exportfile;
53 $exportfile =~ s/^\"(.*)\"$/$1/;
54 open(OUT, ">$exportfile") or die "Can not open $exportfile";
55  
56 #if using MacPerl, set creator and type to BBEdit and Text
57 if ($^O eq 'MacOS') {
58 MacPerl::SetFileInfo('R*ch', 'TEXT', $exportfile);
59  
60 }
61  
62  
63 ########## Start extraction #########
64  
65 ############################################
66 # Set start time for main calculation loop #
67 ############################################
68 my $t1 = [Time::HiRes::time()];
69 my $runningrecordcount=0;
70 ###################################################
71  
72 #### Start while loop through records in file #####
73  
74 my @obsoletelines = ();
75 my $linecount = 0;
76 my %validcodes;
77 my %invalidcodes;
78 while (my $line = <IN>) {
79 chomp $line;
80 #only lines with at least 16 spaces are of interest
81 if ($line =~ /[a-z]+( {16} *)/) {
82 #put code and language name in separate spots
83 my @code_geogarea = split $1, $line;
84 my $geogareacode = $code_geogarea[0];
85 my $geogarea = $code_geogarea[1];
86  
87 #check for obsolete code
88 # invalid codes go into %invalidcodes hash
89 if ($geogareacode =~ /^-/) {
90 $geogareacode =~ s/^-//;
91 #add extra spaces to end of short codes
92 if (length ($geogareacode < 7)) {$geogareacode .= "-"x(7-length ($geogareacode))}
93  
94 $invalidcodes{$geogarea} = $geogareacode;
95  
96 push @obsoletelines, join "\t", "-$geogareacode", $geogarea;
97 }
98  
99 # valid codes go into %validcodes hash
100  
101 else {
102 #add extra spaces to end of short codes
103 if (length ($geogareacode < 7)) {$geogareacode .= "-"x(7-length ($geogareacode))}
104  
105 $validcodes{$geogarea} = $geogareacode;
106 print OUT "$geogareacode\t$geogarea\n";}
107 } # if line has 2 characters 4 or more spaces
108  
109 $linecount++;
110 } # while reading lines
111  
112  
113 print OUT join "\n", @obsoletelines, "\n";
114 print OUT "-"x20, "\nObsolete\tNew\tCountry\n", "-"x20, "\n";
115 foreach my $geogarea (sort keys %invalidcodes) {
116 print OUT "$invalidcodes{$geogarea}\t$validcodes{$geogarea}\t$geogarea\n";
117 }
118 print OUT "\n__GeogAreaCodes__\n";
119  
120 #print tab-separated line of all valid codes
121 foreach my $key (sort keys %validcodes) {
122 print OUT $validcodes{$key}, "\t";
123 }
124 print OUT "\n__ObsoleteGeogAreaCodes__\n";
125 #print tab-separated line of all invalid codes
126 foreach my $key (sort keys %invalidcodes) {
127 print OUT $invalidcodes{$key}, "\t";
128 }
129  
130 print "$linecount lines cleaned\n";
131  
132 close IN;
133 close OUT;
134 print "\n\nPress Enter to quit";
135 <>;
136  
137  
138 #####################
139 ### END OF PROGRAM ##
140 #####################
141  
142 =head1 LICENSE
143  
144 This code may be distributed under the same terms as Perl itself.
145  
146 Please note that this code is not a product of or supported by the
147 employers of the various contributors to the code.
148  
149 =head1 AUTHOR
150  
151 Bryan Baldus
152 eijabb@cpan.org
153  
154 Copyright (c) 2003-2004
155  
156 =cut