script/languagecodelistclean.pl untested

filter: Statementstmt 0.0% 0 / 61 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 / 76 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 Language Code List Cleanup
6  
7 =head2 DESCRIPTION
8  
9 Reads MARC code list for languages (plain text--ASCII version L<http://www.loc.gov/marc/>).
10 Writes only codes and language 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 Language 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 19 spaces are of interest
81 if ($line =~ /[a-z]{3}( {19} *)/) {
82 #put code and language name in separate spots
83 my @code_lang = split $1, $line;
84 my $langcode = $code_lang[0];
85 my $language = $code_lang[1];
86  
87 #check for obsolete code
88 # invalid codes go into %invalidcodes hash
89 if ($langcode =~ /^-/) {
90 $langcode =~ s/-//;
91 $invalidcodes{$language} = $langcode;
92  
93 push @obsoletelines, join "\t", @code_lang;
94 }
95  
96 # valid codes go into %validcodes hash
97  
98 else {
99 $validcodes{$language} = $langcode;
100 print OUT "$langcode\t$language\n";}
101 } # if line has 3 characters 19 or more spaces
102  
103 $linecount++;
104 } # while reading lines
105  
106  
107 print OUT join "\n", @obsoletelines, "\n";
108 print OUT "-"x20, "\nObsolete\tNew\tLanguage\n", "-"x20, "\n";
109 foreach my $lang (sort keys %invalidcodes) {
110 print OUT "$invalidcodes{$lang}\t$validcodes{$lang}\t$lang\n";
111 }
112  
113 print OUT "\n__LanguageCodes__\n";
114  
115 #print tab-separated line of all valid codes
116 foreach my $key (sort keys %validcodes) {
117 print OUT $validcodes{$key}, "\t";
118 }
119 print OUT "\n__ObsoleteLanguageCodes__\n";
120 #print tab-separated line of all invalid codes
121 foreach my $key (sort keys %invalidcodes) {
122 print OUT $invalidcodes{$key}, "\t";
123 }
124  
125  
126 print "$linecount lines cleaned\n";
127 close IN;
128 close OUT;
129 print "\n\nPress Enter to quit";
130 <>;
131  
132  
133 #####################
134 ### END OF PROGRAM ##
135 #####################
136  
137 =head1 LICENSE
138  
139 This code may be distributed under the same terms as Perl itself.
140  
141 Please note that this code is not a product of or supported by the
142 employers of the various contributors to the code.
143  
144 =head1 AUTHOR
145  
146 Bryan Baldus
147 eijabb@cpan.org
148  
149 Copyright (c) 2003-2004
150  
151 =cut