script/countrycodelistclean.pl untested

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 3 Podpod n/a totaltotal 0.0% 0 / 78 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 Country Code List Cleanup
6  
7 =head2 DESCRIPTION
8  
9 Reads MARC code list for countries (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 PURPOSE
14  
15 After cleaning, copy the codes (obsolete, valid, (and match-obsolete to valid)) into a validation-check program/module as data.
16  
17 =head2 TO DO
18  
19 Remove OUT2? I think this is just for error checking/testing?
20  
21 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.
22 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.
23  
24 =cut
25  
26 ###########################
27 ### Initialize includes ###
28 ### and basic needs ###
29 ###########################
30 use strict;
31 #use Sort::Fields;
32 use MARC::BBMARC;
33 ##########################
34 ## Time coding routines ##
35 ## Print start time and ##
36 ## set start variable ##
37 ##########################
38  
39 use Time::HiRes qw( tv_interval );
40 # measure elapsed time
41 my $t0 = [Time::HiRes::time()];
42 my $startingtime = MARC::BBMARC::startstop_time();
43 #########################
44 ### Start main program ##
45 #########################
46  
47 print ("Welcome to Country Code List Cleanup\n");
48  
49 ##### File handling initialization ######
50 #prompt for input file
51 print ("What is the input file? ");
52 my $inputfile=<>;
53 chomp $inputfile;
54 $inputfile =~ s/^\"(.*)\"$/$1/;
55 open (IN, "<$inputfile") or die "Can not open $inputfile";
56 print ("What is the export file? ");
57 my $exportfile = <>;
58 chomp $exportfile;
59 $exportfile =~ s/^\"(.*)\"$/$1/;
60 open(OUT, ">$exportfile") or die "Can not open $exportfile";
61  
62 #if using MacPerl, set creator and type to BBEdit and Text
63 if ($^O eq 'MacOS') {
64 MacPerl::SetFileInfo('R*ch', 'TEXT', $exportfile);
65 }
66  
67  
68 ########## Start extraction #########
69  
70 ############################################
71 # Set start time for main calculation loop #
72 ############################################
73 my $t1 = [Time::HiRes::time()];
74 my $runningrecordcount=0;
75 ###################################################
76  
77 #### Start while loop through records in file #####
78  
79 my @obsoletelines = ();
80 my $linecount = 0;
81 my %validcodes;
82 my %invalidcodes;
83 while (my $line = <IN>) {
84 chomp $line;
85 #only lines with at least 4 spaces are of interest
86 if ($line =~ /[a-z]{2}( {4} *)/) {
87 #put code and language name in separate spots
88 my @code_country = split $1, $line;
89 my $countrycode = $code_country[0];
90 my $country = $code_country[1];
91  
92 #check for obsolete code
93 # invalid codes go into %invalidcodes hash
94 if ($countrycode =~ /^-/) {
95 #remove minus sign from front of invalid code
96 $countrycode =~ s/-//;
97 #add space to two character codes
98 $countrycode =~ s/^([a-z][a-z])$/$1 /;
99 $invalidcodes{$country} = $countrycode;
100 #put the invalid code\tcountry in obsolete lines array
101 push @obsoletelines, join "\t", @code_country;
102 }
103  
104 # valid codes go into %validcodes hash
105 else {
106 #add space to two character codes
107 $countrycode =~ s/^([a-z][a-z])$/$1 /;
108 $validcodes{$country} = $countrycode;
109 print OUT "$countrycode\t$country\n";}
110 } # if line has 2 characters 4 or more spaces
111  
112 $linecount++;
113 } # while reading lines
114  
115 #print obsolete codes after valid codes have printed
116 print OUT join "\n", @obsoletelines, "\n";
117 #next try to match obsolete with valid based on country
118 print OUT "-"x20, "\nObsolete\tNew\tCountry\n", "-"x20, "\n";
119 foreach my $country (sort keys %invalidcodes) {
120 print OUT "$invalidcodes{$country}\t$validcodes{$country}\t$country\n";
121 }
122 print OUT "\n__CountryCodes__\n";
123  
124 #print tab-separated line of all valid codes
125 foreach my $key (sort keys %validcodes) {
126 print OUT $validcodes{$key}, "\t";
127 }
128 print OUT "\n__ObsoleteCountry__\n";
129 #print tab-separated line of all invalid codes
130 foreach my $key (sort keys %invalidcodes) {
131 print OUT $invalidcodes{$key}, "\t";
132 }
133  
134 print "$linecount lines cleaned\n";
135  
136 close IN;
137 close OUT;
138 print "\n\nPress Enter to quit";
139 <>;
140  
141  
142 #####################
143 ### END OF PROGRAM ##
144 #####################
145  
146 =head1 LICENSE
147  
148 This code may be distributed under the same terms as Perl itself.
149  
150 Please note that this code is not a product of or supported by the
151 employers of the various contributors to the code.
152  
153 =head1 AUTHOR
154  
155 Bryan Baldus
156 eijabb@cpan.org
157  
158 Copyright (c) 2003-2004
159  
160 =cut