make the fingerprinter normalize OCLC control numbers
[migration-tools.git] / Equinox-Migration / lib / Equinox / Migration / Utils.pm
1 package Equinox::Migration::Utils;
2
3 # Copyright 2014, Equinox Software, Inc.
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18
19 use strict;
20 use warnings;
21
22 BEGIN {
23     require Exporter;
24     
25     our $VERSION =   1.00;
26     our @ISA     =   qw(Exporter);
27     our @EXPORT  =   ();
28     our @EXPORT_OK = qw(normalize_oclc_number);
29 }
30
31 sub normalize_oclc_number {
32     my $str = shift;
33   
34     # trim
35     $str =~ s/^\s+//;
36     $str =~ s/\s+$//;
37
38     # get rid of prefixes
39     $str =~ s/^\(OCoLC\)//i;
40     $str =~ s/^(ocl7|ocm|ocn|on)//i; 
41
42     # ... and any leading zeroes
43     $str =~ s/^0+//;
44
45     if ($str =~ /^\d+$/) {
46         return '(OCoLC)' . $str;
47     } else {
48         return;
49     }
50 }
51
52 =head1 NAME
53
54 Equinox::Migration::Utils - utility functions
55
56 =head1 SYNOPSIS
57
58   use Equinox::Migration::Utils qw/normalize_oclc_number/;
59   my $normalized = normalize_oclc_number($oclc);
60
61 =head1 FUNCTIONS
62
63 =head2 normalize_oclc_number)
64
65   my $normalized = normalize_oclc_number($oclc);
66
67 Returns a normalized form of a string that is assumed to be
68 an OCLC control number. The normalized form consists of the
69 string "(OCoLC)" followed by the numeric portion of the OCLC
70 number, sans leading zeroes.
71
72 The input string is expected to be a sequence of digits with
73 optional leading and trailing whitespace and an optional prefix
74 from a set observed in the wild, e.g., "(OCoLC)", "ocm", and so
75 forth. If the input string does not meet this condition, the
76 undefined value is returned.
77
78 =head1 AUTHOR
79
80 Galen Charlton
81
82 =head1 COPYRIGHT
83
84 Copyright 2014, Equinox Software Inc.
85
86 =cut
87
88 1;