#!/usr/bin/perl -w # lccnorm by Paul Hoffman (nkuitse AT nkuitse DOT com). # Copyright 2007 Paul M. Hoffman. # This software is made available under the same terms as Perl; # see below for details. use strict; # -------------------------------- Required modules use Biblio::LCC; use File::Basename qw(basename); use Getopt::Long qw(:config posix_default gnu_compat require_order bundling no_ignore_case); # -------------------------------- Variables use vars qw($PROGRAM $VERSION $AUTHOR $COPYRIGHT $PAGER); $PROGRAM = basename($0); $VERSION = '0.08'; $AUTHOR = 'Paul Hoffman (nkuitse AT nkuitse DOT com)'; $COPYRIGHT = "Copyright 2007 Paul M. Hoffman"; my $action = \&run; my $verbose; my $in_delim = "\t"; my $out_join; my ($i, $j) = (0, 0); # Field(s) that contain the call no. (or range) my ($output_pos, $copy, $at_beginning, $at_end, $input_is_ranges, $errors_are_fatal); # -------------------------------- Setup $| = 1; # -------------------------------- Read command-line options and arguments GetOptions( 'f|fields=s' => sub { my $field_spec = $_[1]; if ($field_spec =~ /^(\d+)-(\d+)$/) { ($i, $j) = ($1 - 1, $2 - 1); exit usage("Invalid field spec: end must be greater than beginning") unless $i < $j; exit usage("Invalid field spec: must be 1, 2, 3, or 4 fields") unless $j - $i < 4; } elsif ($field_spec =~ /^(\d+)$/) { ($i, $j) = ($1 - 1, $1 - 1); } }, 'r|ranges' => \$input_is_ranges, 'd|delimiter=s' => \$in_delim, 'j|join=s' => \$out_join, 'c|copy' => \$copy, 'b|beginning' => \$at_beginning, 'e|end' => \$at_end, 'D|die-on-error' => \$errors_are_fatal, 'v|verbose' => \$verbose, 'h|help' => sub { $action = \&help }, 'V|version' => sub { $action = \&version }, 'M|manual' => sub { $action = \&manual }, 'L|license' => sub { $action = \&license }, ) or exit usage(); $out_join = $in_delim unless defined $out_join; $in_delim = qr/\Q$in_delim\E/ if defined $in_delim; if ($copy) { exit usage("Must specify -b|--beginning or -e|--end when using option -c|--copy") unless $at_beginning or $at_end; } # -------------------------------- Perform the desired action $action->(); # -------------------------------- Action functions sub run { # --- Figure out how to convert fields to a range string # Number of fields that hold the call number or range my $n = $j - $i + 1; # Function to construct an (unnormalized) call number or range from the input fields my $field2str; if ($n == 1) { # Ranges: ("E184-E184.5") --> "E184-E184.5" # Call #: ("Z699.22 .H54 1982") $field2str = sub { $_[0] }; } elsif ($n == 2) { # Ranges: ("E184", "E184.5") --> "E184-E184.5" # Call #: ("Z", "699.22 .H54 1982") or some such $field2str = $input_is_ranges ? sub { join('-', @_) } : sub { join(' ', @_) }; } elsif ($n == 3) { # ("E", "184", "184.5") --> "E184-E184.5" # Call #: ("Z", "699.22", ".H54 1982") or some such $field2str = $input_is_ranges ? sub { "$_[0] $_[1]-$_[0] $_[2]" } : sub { join(' ', @_) }; } elsif ($n == 4) { # ("E", "184", "E", "184.5") --> "E184-E184.5" # Call #: ("Z", "699.22", ".H54", "1982") or some such $field2str = $input_is_ranges ? sub { "$_[0] $_[1]-$_[2] $_[3]" } : sub { join(' ', @_) }; } else { die "This can't happen, because we eliminated this possibility earlier"; } # --- Process the input (finally!) my $line_num = 1; while (<>) { chomp; my $line = $_; # --- Split the line up into fields my @fields = split /$in_delim/; # --- Isolate the fields that hold the call number (or range) from # those (if any) that precede and follow them my (@before, @callnum, @after); @before = splice @fields, 0, $i if $i > 0; @callnum = splice @fields, 0, $n; @after = @fields; my @result; # --- Normalize the call number or range if ($input_is_ranges) { my $range = $field2str->(@callnum); eval { @result = Biblio::LCC->normalize_range($range); }; } else { my $callnum = $field2str->(@callnum); eval { @result = (Biblio::LCC->normalize($callnum)); }; } if ($@) { print STDERR "Error in input line $line_num: $line\n"; exit 2 if $errors_are_fatal; next; } my @output; if ($copy) { @output = $at_beginning ? (@result, @before, @callnum, @after) : ( @before, @callnum, @after, @result) ; } else { @output = $at_beginning ? (@result, @before, @after) : $at_end ? ( @before, @after, @result ) : ( @before, @result, @after ) ; } print join($out_join, @output), "\n"; } continue { $line_num++; } } sub help { usage(); options(); exit 0; } sub version { print "This is $PROGRAM " if $verbose; print $VERSION; print <<"EOS" if $verbose; by $AUTHOR. $COPYRIGHT. This is free software, made available under the same terms as Perl itself. EOS print "\n" unless $verbose; exit 0; } sub manual { system(podder()) == 0 or print STDERR "I can't find a way to print the manual page for $PROGRAM.\n"; } sub license { my $pager = pager(); if ($pager and which('sed')) { system(qq{sed '/^This software is made available/,/^The End/!d' "$0" | "$pager"}); } else { print STDERR "I can't find a way to print the license for $PROGRAM\n"; } } # -------------------------------- Other functions sub options { print <<"EOS"; Options: -f|--fields RANGE Field(s) containing the call number (or range) -r|--ranges Input is ranges -d|--delimiter STR Input delimiter (default: tab) -j|--join STR Output delimiter (default: same as for input) -c|--copy Don't delete source fields -b|--beginning Put normalized string at beginning of line -e|--end Put normalized string at end of line -D|--die-on-error Exit with an error as soon as an error is encountered -v|--verbose Be verbose -h|--help Print help and exit -V|--version Print version and exit -M|--manual Print manual (using pager) and exit -L|--license Print license (using pager) and exit EOS } sub usage { print STDERR "$_\n" foreach @_; print <<"EOS"; Usage: $PROGRAM [OPTION...] [FILE...] EOS return 1; } sub pager { return $PAGER if defined $PAGER; $PAGER = $ENV{'PAGER'} || which(qw(less more)) || which('cat') || 0; return $PAGER; } sub podder { return "cat $0 | pod2man -n $PROGRAM -s 1 -r $VERSION -c '' | nroff -man | " . pager() if pager() and which('pod2man') and which('nroff'); return "man 1 $PROGRAM" if manpage($PROGRAM); return "perldoc -F $0" if which('perldoc'); return undef; } sub which { my $out; no warnings; foreach my $prog (@_) { $out = `which $prog 2>/dev/null`; if (defined $out) { chomp $out; return $out if -x $out; } } return undef; } sub manpage { return unless which('man'); my $out = `man -w 1 $PROGRAM 2>/dev/null`; return unless defined $out; chomp $out; return $out; } =head1 NAME lccnorm - normalize Library of Congress Classification call numbers =head1 SYNOPSIS B [I