#!/usr/bin/perl
use strict;
use warnings;
use SOAP::Lite;
use XML::Simple;
use Getopt::Long;
use Pod::Usage;
=head1 NAME
naf - Lookup a name in the LC Name Authority File
=head1 SYNOPSIS
% naf --name="Seuss"
=head1 DESCRIPTION
OCLC's ResearchWorks L
offers an experiemtal Web Service for the LC Name Authority File.
C is a command line tool that allows you to lookup a name
and have matches printed to STDOUT.
=head1 OPTIONS
=head2 --max
Indicates the maximum amount of hits to include in the search result.
Defaults to 100.
=head1 AUTHORS
=over 4
=item * Ed Summers
=cut
## gather options
my ( $name, $max );
GetOptions(
'name:s' => \$name,
'max:s' => \$max,
);
## set defaults
$max = 100 if ! $max;
pod2usage( { verbose => 2 } ) if ! $name;
## submit request
my $result = SOAP::Lite
->proxy( 'http://alcme.oclc.org/eprintsUK/services/NACOMatch' )
->getNameAuthority(
SOAP::Data->name( 'name' => $name ),
SOAP::Data->name( 'maxList' => $max ),
)
->result();
## parse xml response
my $names = XMLin( $result );
## in case of utf8
binmode( STDOUT, ':utf8' );
## no results
if ( $names->{ hitCount } == 0 ) {
print "no matches found for $name\n";
}
## one result
elsif ( $names->{ hitCount } == 1 ) {
print $names->{match}{establishedForm},"\n";
}
## more than one result
else {
foreach my $match ( @{ $names->{ match } } ) {
print $match->{ establishedForm }, "\n";
}
}