Skip to content

Commit

Permalink
Add support for leader
Browse files Browse the repository at this point in the history
  • Loading branch information
michal-josef-spacek committed Jan 28, 2025
1 parent c88ba3a commit 7ec96bc
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
0.05
- Add -f option to print frequency.
- Add support for leader.
- Improve doc in marc-list.
- Remove pointless uniq from code.

Expand Down
28 changes: 17 additions & 11 deletions List.pm
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,16 @@ sub run {
print STDERR "\t-h\t\tPrint help.\n";
print STDERR "\t--version\tPrint version.\n";
print STDERR "\tmarc_xml_file\tMARC XML file.\n";
print STDERR "\tfield\t\tMARC field.\n";
print STDERR "\tfield\t\tMARC field (field number or 'leader' string).\n";
print STDERR "\tsubfield\tMARC subfield (for datafields).\n";
return 1;
}
$self->{'_marc_xml_file'} = shift @ARGV;
$self->{'_marc_field'} = shift @ARGV;
$self->{'_marc_subfield'} = shift @ARGV;

if (int($self->{'_marc_field'}) > 9
if ($self->{'_marc_field'} ne 'leader'
&& int($self->{'_marc_field'}) > 9
&& ! defined $self->{'_marc_subfield'}) {

err 'Subfield is required.';
Expand Down Expand Up @@ -82,16 +83,21 @@ sub run {
}
$previous_record = $record;

my @fields = $record->field($self->{'_marc_field'});
foreach my $field (@fields) {
if (defined $self->{'_marc_subfield'}) {
my @subfield_values = $field->subfield($self->{'_marc_subfield'});
foreach my $subfield_value (@subfield_values) {
$ret_hr->{$subfield_value}++;
if ($self->{'_marc_field'} eq 'leader') {
my $leader = $record->leader;
$ret_hr->{"'".$leader."'"}++;
} else {
my @fields = $record->field($self->{'_marc_field'});
foreach my $field (@fields) {
if (defined $self->{'_marc_subfield'}) {
my @subfield_values = $field->subfield($self->{'_marc_subfield'});
foreach my $subfield_value (@subfield_values) {
$ret_hr->{$subfield_value}++;
}
} else {
my $data = $field->data;
$ret_hr->{$data}++;
}
} else {
my $data = $field->data;
$ret_hr->{$data}++;
}
}
$num++;
Expand Down
4 changes: 3 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ TODO
====
3. Support multiple params. marc-list 490 a 490 b
4. Fix message of issue with MARC record.
5. Add support for leader.
7. Add check for input field (field or leader string).
8.

Expand All @@ -17,3 +16,6 @@ OK [20220518] skim

6. Add freq to output structure. Add option to sort via freq.
OK [20250128] skim

5. Add support for leader.
OK [20250128] skim
21 changes: 19 additions & 2 deletions t/App-MARC-List/04-run.t
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use English;
use Error::Pure::Utils qw(clean);
use File::Object;
use File::Spec::Functions qw(abs2rel);
use Test::More 'tests' => 12;
use Test::More 'tests' => 13;
use Test::NoWarnings;
use Test::Output;
use Test::Warn;
Expand Down Expand Up @@ -143,6 +143,23 @@ stdout_is(
'Run list for MARC XML file with 1 record (260b, with frequency).',
);

# Test.
@ARGV = (
$data_dir->file('ex3.xml')->s,
'leader',
);
$right_ret = <<'END';
' nam a22 4500'
END
stdout_is(
sub {
App::MARC::List->new->run;
return;
},
$right_ret,
'Run list for MARC XML file with 1 record (260b, with frequency).',
);

# Test.
@ARGV = (
$data_dir->file('ex2.xml')->s,
Expand Down Expand Up @@ -181,7 +198,7 @@ Usage: $script [-f] [-h] [--version] marc_xml_file field [subfield]
-h Print help.
--version Print version.
marc_xml_file MARC XML file.
field MARC field.
field MARC field (field number or 'leader' string).
subfield MARC subfield (for datafields).
END

Expand Down

0 comments on commit 7ec96bc

Please sign in to comment.