App-MARC-Record-Stats
view release on metacpan or search on metacpan
package App::MARC::Record::Stats;
use strict;
use warnings;
use Class::Utils qw(set_params);
use English;
use Error::Pure qw(err);
use Getopt::Std;
use List::Util 1.33 qw(none);
use MARC::File::XML (BinaryEncoding => 'utf8', RecordFormat => 'MARC21');
use MARC::Record::Stats;
use Readonly;
use Unicode::UTF8 qw(encode_utf8);
Readonly::Array our @INPUT_FORMATS => qw(xml);
our $VERSION = 0.02;
# Constructor.
sub new {
my ($class, @params) = @_;
# Create object.
my $self = bless {}, $class;
# Process parameters.
set_params($self, @params);
# Object.
return $self;
}
# Run.
sub run {
my $self = shift;
# Process arguments.
$self->{'_opts'} = {
'h' => 0,
'i' => 'xml',
};
if (! getopts('hi:', $self->{'_opts'}) || @ARGV < 1
|| $self->{'_opts'}->{'h'}) {
print STDERR "Usage: $0 [-h] [-i format] [--version] marc_file\n";
print STDERR "\t-h\t\tPrint help.\n";
print STDERR "\t-i format\tInput MARC format. Possible formats is xml (default values is xml).\n";
print STDERR "\t--version\tPrint version.\n";
print STDERR "\tmarc_file\tMARC file.\n";
return 1;
}
$self->{'_marc_file'} = shift @ARGV;
# Check output format.
if (none { $self->{'_opts'}->{'i'} eq $_ } @INPUT_FORMATS) {
err "Input format '$self->{'_opts'}->{'i'}' doesn't supported.";
}
my $stats = MARC::Record::Stats->new;
my $marc_file;
if ($self->{'_opts'}->{'i'} eq 'xml') {
$marc_file = MARC::File::XML->in($self->{'_marc_file'});
if (! defined $marc_file) {
err 'Cannot create object for MARC file.';
}
} else {
err 'Bad input MARC file.';
}
my $num = 1;
my $previous_record;
while (1) {
my $record = eval {
$marc_file->next;
};
if ($EVAL_ERROR) {
print STDERR "Cannot process '$num' record. ".
(
defined $previous_record
? "Previous record is ".encode_utf8($previous_record->title)."\n"
: ''
);
print STDERR "Error: $EVAL_ERROR\n";
next;
}
if (! defined $record) {
last;
}
$stats->add_record_to_stats($record);
$previous_record = $record;
$num++;
}
$marc_file->close;
# Print out.
$stats->report(*STDOUT);
return 0;
}
1;
__END__
=pod
=encoding utf8
=head1 NAME
( run in 0.529 second using v1.01-cache-2.11-cpan-f56aa216473 )