Skip to content

Instantly share code, notes, and snippets.

@tyru
Created April 22, 2009 16:46
Show Gist options
  • Select an option

  • Save tyru/99905 to your computer and use it in GitHub Desktop.

Select an option

Save tyru/99905 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use Encode;
use FileHandle;
use Getopt::Long;
use Pod::Usage;
my ($needhelp, $listfile, $listall, $fileencoding);
$fileencoding = 'utf8'; # isbn-list.txt‚̃Gƒ“ƒR[ƒfƒBƒ“ƒO
GetOptions(
'help' => \$needhelp,
'file' => \$listfile,
'encoding=s' => \$fileencoding,
'all' => \$listall,
) or pod2usage(-verbose => 1);
pod2usage(-verbose => 1) if $needhelp;
my $search_isbn = shift;
if (! defined $search_isbn && ! defined $listall) {
die "specify ISBN.\n";
}
$listfile ||= 'isbn-list.txt';
if (! -r $listfile) {
die "$listfile: No such file\n";
}
# determine encoding.
my $encoding = $^O eq 'MSWin32' ? 'cp932' : 'utf8';
if (exists $ENV{LANG} && $ENV{LANG} =~ /^([^.]+)\.([^.]+)$/) {
$encoding = $2;
}
my $FH = FileHandle->new($listfile);
unless (defined $FH) {
die "$listfile: Can't open file\n";
}
while (<$FH>) {
chomp;
next if /^\s*$/;
$_ = decode $fileencoding, $_;
if (/^([^\t]*)\t([^\t]*)$/) {
my ($isbn, $title) = ($1, $2);
if ($listall) {
print encode $encoding, "$isbn:\n $title\n\n";
} else {
if ($search_isbn eq $isbn) {
print encode($encoding, $title) . "\n";
exit;
}
}
} else {
warn "$.: bad form of list file\n";
}
}
__END__
=head1 NAME
isbn-lookup.pl - looking up the title of ISBN.
=head1 OPTIONS
=over 1
=item -a, --all
show all the list file of ISBN and that title.
=item -f, --file
specify list file.
=item -e, --encoding
specify list file encoding.
=item -h, --help
show this help.
=back
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment