File Coverage

blib/lib/MARC/Leader/Utils.pm
Criterion Covered Total %
statement 45 46 97.8
branch 17 18 94.4
condition 11 12 91.6
subroutine 16 16 100.0
pod 2 2 100.0
total 91 94 96.8


line stmt bran cond sub pod time code
1             package MARC::Leader::Utils;
2              
3 4     4   206285 use base qw(Exporter);
  4         9  
  4         607  
4 4     4   26 use strict;
  4         8  
  4         111  
5 4     4   21 use warnings;
  4         9  
  4         273  
6              
7 4     4   1542 use Error::Pure qw(err);
  4         30479  
  4         166  
8 4     4   318 use List::Util 1.33 qw(any);
  4         129  
  4         478  
9 4     4   59 use Scalar::Util qw(blessed);
  4         9  
  4         320  
10 4     4   25 use Readonly;
  4         8  
  4         2586  
11              
12             # Constants.
13             Readonly::Array our @EXPORT => qw(check_material_type material_type);
14             Readonly::Array our @MATERIAL_TYPES => qw(book computer_file continuing_resource map
15             mixed_material music visual_material);
16              
17             our $VERSION = 0.02;
18              
19             sub check_material_type {
20 8     8 1 391287 my $material_type = shift;
21              
22 8 100   35   104 if (any { $material_type eq $_ } @MATERIAL_TYPES) {
  35         232  
23 7         60 return 1;
24             } else {
25 1         10 return 0;
26             }
27             }
28              
29             sub material_type {
30 11     11 1 376022 my $leader = shift;
31              
32 11 100 100     110 if (! defined $leader
      100        
33             || ! blessed($leader)
34             || ! $leader->isa('Data::MARC::Leader')) {
35              
36 3         67 err "Leader object must be a Data::MARC::Leader instance.";
37             }
38              
39 8         16 my $material_type;
40 8 100 100 14   60 if ((any { $leader->type eq $_ } qw(a t))
  14 100 66     127  
    100          
    100          
    100          
    100          
    50          
41 8     8   82 && (any { $leader->bibliographic_level eq $_ } qw(a c d m))) {
42              
43 1         11 $material_type = 'book';
44             } elsif ($leader->type eq 'm') {
45 1         15 $material_type = 'computer_file';
46 11     11   145 } elsif (any { $leader->type eq $_ } qw(e f)) {
47 2         15 $material_type = 'map';
48 13     13   97 } elsif (any { $leader->type eq $_ } qw(c d i j)) {
49 1         8 $material_type = 'music';
50             } elsif ($leader->type eq 'a'
51 3     3   33 && (any { $leader->bibliographic_level eq $_ } qw(b i s))) {
52              
53 1         10 $material_type = 'continuing_resource';
54 6     6   67 } elsif (any { $leader->type eq $_ } qw(g k o r)) {
55 1         43 $material_type = 'visual_material';
56             } elsif ($leader->type eq 'p') {
57 1         16 $material_type = 'mixed_material';
58             } else {
59 0         0 err "Unsupported material type.";
60             }
61              
62 8         65 return $material_type;
63             }
64              
65             1;
66              
67             __END__
68              
69             =pod
70              
71             =encoding utf8
72              
73             =head1 NAME
74              
75             MARC::Leader::Utils - Utilities for MARC::Leader.
76              
77             =head1 SYNOPSIS
78              
79             use MARC::Leader::Utils qw(material_type);
80              
81             my $bool = check_material_type($material_type);
82             my $material_type = material_type($leader_obj);
83              
84             =head1 DESCRIPTION
85              
86             The Perl module with common utilities for work with MARC leader field.
87              
88             =head1 SUBROUTINES
89              
90             =head2 C<check_material_type>
91              
92             my $bool = check_material_type($material_type);
93              
94             Check if material type string is valid.
95              
96             Possible strings are book, computer_file, continuing_resource, map, mixed_material, music and visual_material.
97              
98             Returns 0/1.
99              
100             =head2 C<material_type>
101              
102             my $material_type = material_type($leader_obj);
103              
104             Get material type.
105             This process is defined in MARC 008 field.
106              
107             C<$leader_obj> variable is L<Data::MARC::Leader> instace.
108              
109             Returned strings are:
110              
111             =over 8
112              
113             =item * book
114              
115             =item * computer_file
116              
117             =item * continuing_resource
118              
119             =item * map
120              
121             =item * mixed_material
122              
123             =item * music
124              
125             =item * visual_material
126              
127             =back
128              
129             Returns string.
130              
131             =head1 ERRORS
132              
133             material_type():
134             Leader object must be a Data::MARC::Leader instance.
135             Unsupported material type.
136              
137             =head1 EXAMPLE1
138              
139             =for comment filename=check_material_type.pl
140              
141             use strict;
142             use warnings;
143              
144             use MARC::Leader::Utils qw(check_material_type);
145              
146             if (@ARGV < 1) {
147             print STDERR "Usage: $0 material_type\n";
148             exit 1;
149             }
150             my $material_type = $ARGV[0];
151              
152             my $ret = check_material_type($material_type);
153              
154             print "Expected material type: $material_type\n";
155             print "Result: $ret\n";
156              
157             # Output (book):
158             # Expected material type: book
159             # Result: 1
160              
161             # Output (foo):
162             # Expected material type: foo
163             # Result: 0
164              
165             =head1 EXAMPLE2
166              
167             =for comment filename=material_type.pl
168              
169             use strict;
170             use warnings;
171              
172             use MARC::Leader;
173             use MARC::Leader::Utils qw(material_type);
174              
175             if (@ARGV < 1) {
176             print STDERR "Usage: $0 leader_string\n";
177             exit 1;
178             }
179             my $leader_string = $ARGV[0];
180              
181             my $leader = MARC::Leader->new->parse($leader_string);
182              
183             my $material_type = material_type($leader);
184              
185             print "Leader: |$leader_string|\n";
186             print "Material type: $material_type\n";
187              
188             # Output for ' nem a22 2 4500':
189             # Leader: | nem a22 2 4500|
190             # Material type: map
191              
192             =head1 DEPENDENCIES
193              
194             L<Error::Pure>,
195             L<Exporter>,
196             L<File::Spec::Functions>,
197             L<File::Share>,
198             L<Readonly>.
199              
200             =head1 SEE ALSO
201              
202             =over
203              
204             =item L<Data::MARC::Leader>
205              
206             Data object for MARC leader.
207              
208             =item L<MARC::Leader>
209              
210             MARC leader class.
211              
212             =back
213              
214             =head1 REPOSITORY
215              
216             L<https://github.com/michal-josef-spacek/MARC-Leader-Utils>
217              
218             =head1 AUTHOR
219              
220             Michal Josef Špaček L<mailto:skim@cpan.org>
221              
222             L<http://skim.cz>
223              
224             =head1 LICENSE AND COPYRIGHT
225              
226             © 2025 Michal Josef Špaček
227              
228             BSD 2-Clause License
229              
230             =head1 VERSION
231              
232             0.02
233              
234             =cut