blib/lib/MARC/Leader/Print.pm

filter: Statementstmt 91.6% 77 / 84 Branchbran 66.6% 12 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 100% 2 / 2 totaltotal 88.7% 103 / 116 CC 10 SCAR 13.5
CC
10
Cov
87%
new
8.7
_key
2.0
print
2.0
CRAP 3.8
↑ Summary
1 package MARC::Leader::Print;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use Class::Utils qw(set_params);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Error::Pure qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use MARC::Leader::L10N 0.03;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Mo::utils 0.06 qw(check_bool);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Mo::utils::Language 0.05 qw(check_language_639_1);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 our $VERSION = 0.08;
14  
15 # Constructor.
16 sub new {
17 5 ▶ my ($class, @params) = @_;
Subroutine new: called CC=7 SCAR=21.6
Pod documented
18  
19 # Create object.
20 5 my $self = bless {}, $class;
21  
22 # Language.
23 5 $self->{'lang'} = undef;
24  
25 # Use with ANSI sequences.
26 5 $self->{'mode_ansi'} = undef;
27  
28 # Use description.
29 5 $self->{'mode_desc'} = 1;
30  
31 # Output separator.
32 5 $self->{'output_separator'} = "\n";
33  
34 # Process parameters.
35 5 set_params($self, @params);
36  
37 # Check 'lang'.
38 5 check_language_639_1($self, 'lang');
39  
40 # Check 'mode_ansi'.
41 4 check_bool($self, 'mode_ansi');
42  
43 # Check 'mode_desc'.
44 4 check_bool($self, 'mode_desc');
45  
46 4 ▶ if (! defined $self->{'mode_ansi'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"mode_ansi"}) 1 3 4
47 1 ▶ if (exists $ENV{'NO_COLOR'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $ENV{'NO_COLOR'}) { } 0 1 1
elsif (defined $ENV{'COLOR'}) { } 0 1 1
48 0 $self->{'mode_ansi'} = 0;
49 } elsif (defined $ENV{'COLOR'}) {
50 0 $self->{'mode_ansi'} = 1;
51 } else {
52 1 $self->{'mode_ansi'} = 0;
53 }
54 }
55  
56 # Check routine for ANSI colors output.
57 4 ▶ if ($self->{'mode_ansi'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"mode_ansi"}) 0 4 4
58 0 eval {
59 0 require Term::ANSIColor;
60 };
61 0 ▶ if ($EVAL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($MARC::Leader::Print::EVAL_ERROR) 0 0 0
62 0 err "Cannot load 'Term::ANSIColor' module.";
63 }
64 }
65  
66 4 ▶ $self->{'_l'} = MARC::Leader::L10N->get_handle(defined $self->{'lang'} ? $self->{'lang'} : ());
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'lang'} ? : 3 1 4
67  
68 4 return $self;
69 }
70  
71 sub print {
72 3 ▶ my ($self, $leader_obj) = @_;
Subroutine print: called CC=2 SCAR=6.9
Pod documented
73  
74 3 my @ret;
75 3 push @ret, $self->_key('Record length').$leader_obj->length;
76 3 push @ret, $self->_key('Record status').$self->_print($leader_obj, 'status');
77 3 push @ret, $self->_key('Type of record').$self->_print($leader_obj, 'type');
78 3 push @ret, $self->_key('Bibliographic level').$self->_print($leader_obj,
79 'bibliographic_level');
80 3 push @ret, $self->_key('Type of control').$self->_print($leader_obj,
81 'type_of_control');
82 3 push @ret, $self->_key('Character coding scheme').$self->_print($leader_obj,
83 'char_coding_scheme');
84 3 push @ret, $self->_key('Indicator count').
85 $leader_obj->indicator_count;
86 3 push @ret, $self->_key('Subfield code count').
87 $leader_obj->subfield_code_count;
88 3 push @ret, $self->_key('Base address of data').$leader_obj->data_base_addr;
89 3 push @ret, $self->_key('Encoding level').$self->_print($leader_obj,
90 'encoding_level');
91 3 push @ret, $self->_key('Descriptive cataloging form').$self->_print($leader_obj,
92 'descriptive_cataloging_form');
93 3 push @ret, $self->_key('Multipart resource record level').$self->_print($leader_obj,
94 'multipart_resource_record_level');
95 3 push @ret, $self->_key('Length of the length-of-field portion').
96 $leader_obj->length_of_field_portion_len;
97 3 push @ret, $self->_key('Length of the starting-character-position portion').
98 $leader_obj->starting_char_pos_portion_len;
99 3 push @ret, $self->_key('Length of the implementation-defined portion').
100 $leader_obj->impl_def_portion_len;
101 3 push @ret, $self->_key('Undefined').$self->_print($leader_obj, 'undefined');
102  
103 3 ▶ return wantarray ? @ret : (join $self->{'output_separator'}, @ret);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 1 3
104 }
105  
106 sub _key {
107 48 ▶ my ($self, $key) = @_;
Subroutine _key: called CC=2 SCAR=7.1
108  
109 48 my $ret;
110 48 my $value = $self->{'_l'}->maketext($key);
111 48 ▶ if ($self->{'mode_ansi'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'mode_ansi'}) { } 0 48 48
112 0 $ret = Term::ANSIColor::color('cyan').$value.Term::ANSIColor::color('reset');
113 } else {
114 48 $ret = $value;
115 }
116 48 $ret .= ': ';
117  
118 48 return $ret;
119 }
120  
121 sub _print {
122 27 ▶ my ($self, $leader_obj, $method_name) = @_;
Subroutine _print: called CC=2 SCAR=6.9
123  
124 27 my $ret_value = $leader_obj->$method_name;
125 27 my $ret;
126 27 ▶ if ($self->{'mode_desc'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'mode_desc'}) { } 9 18 27
127 9 my $ret_value_fixed = $ret_value;
128 9 $ret_value_fixed =~ s/\ /_/msg;
129 9 $ret = $self->{'_l'}->maketext($method_name.'.'.$ret_value_fixed);
130 } else {
131 18 $ret = $ret_value;
132 }
133  
134 27 return $ret;
135 }
136  
137 4 1;
138  
139 __END__
140  
141 =pod
142  
143 =encoding utf8
144  
145 =head1 NAME
146  
147 MARC::Leader::Print - MARC leader class for print.
148  
149 =head1 SYNOPSIS
150  
151 use MARC::Leader::Print;
152  
153 my $obj = MARC::Leader::Print->new(%params);
154 my @ret = $obj->print($leader_obj);
155 my $ret = $obj->print($leader_obj);
156  
157 =head1 METHODS
158  
159 =head2 C<new>
160  
161 my $obj = MARC::Leader->new(%params);
162  
163 Constructor.
164  
165 =over 8
166  
167 =item * C<lang>
168  
169 Language of texts in ISO 639-1 format.
170  
171 Possible values are:
172  
173 =over
174  
175 =item * C<en>
176  
177 =item * C<cs>
178  
179 =back
180  
181 Default value is undef, try to use language from locales.
182  
183 =item * C<mode_ansi>
184  
185 Mode for ANSI color support:
186  
187 1 - ANSI color support enabled.
188 0 - ANSI color support disabled.
189  
190 When is undefined, env variables C<COLOR> or C<NO_COLOR> could control ANSI
191 color support.
192  
193 Default value is undef.
194  
195 =item * C<mode_desc>
196  
197 Use description instead of raw leader values.
198  
199 Default value is 1.
200  
201 =item * C<output_separator>
202  
203 Output separator used in scalar context of C<print()> method.
204  
205 Default value is "\n".
206  
207 =back
208  
209 Returns instance of object.
210  
211 =head2 C<print>
212  
213 my @ret = $obj->print($leader_obj);
214 my $ret = $obj->print($leader_obj);
215  
216 Process L<Data::MARC::Leader> instance to output print.
217 In scalar context compose printing output as one string.
218 In array context compose list of printing lines.
219  
220 Color (ANSI colors) output is controlled by 'mode_ansi' parameter
221 or env variables C<COLOR> and C<NO_COLOR>.
222  
223 Returns string in scalar context.
224 Returns array of string in array context.
225  
226 =head1 ERRORS
227  
228 new():
229 From Class::Utils::set_params():
230 Unknown parameter '%s'.
231 From Mo::utils::check_bool():
232 Parameter 'mode_ansi' must be a bool (0/1).
233 Value: %s
234 Parameter 'mode_desc' must be a bool (0/1).
235 Value: %s
236 From Mo::utils::Language::check_language_639_1():
237 Parameter 'lang' doesn't contain valid ISO 639-1 code.
238 Codeset: %s
239 Value: %s
240 Cannot load 'Term::ANSIColor' module.
241  
242 =head1 EXAMPLE1
243  
244 =for comment filename=print_data_marc_leader.pl
245  
246 use strict;
247 use warnings;
248  
249 use Data::MARC::Leader;
250 use MARC::Leader::Print;
251  
252 # Print object.
253 my $print = MARC::Leader::Print->new(
254 'lang' => 'en',
255 );
256  
257 # Data object.
258 my $data_marc_leader = Data::MARC::Leader->new(
259 'bibliographic_level' => 'm',
260 'char_coding_scheme' => 'a',
261 'data_base_addr' => 541,
262 'descriptive_cataloging_form' => 'i',
263 'encoding_level' => ' ',
264 'impl_def_portion_len' => '0',
265 'indicator_count' => '2',
266 'length' => 2200,
267 'length_of_field_portion_len' => '4',
268 'multipart_resource_record_level' => ' ',
269 'starting_char_pos_portion_len' => '5',
270 'status' => 'c',
271 'subfield_code_count' => '2',
272 'type' => 'e',
273 'type_of_control' => ' ',
274 'undefined' => '0',
275 );
276  
277 # Print to output.
278 print scalar $print->print($data_marc_leader), "\n";
279  
280 # Output:
281 # Record length: 2200
282 # Record status: Corrected or revised
283 # Type of record: Cartographic material
284 # Bibliographic level: Monograph/Item
285 # Type of control: No specified type
286 # Character coding scheme: UCS/Unicode
287 # Indicator count: Number of character positions used for indicators
288 # Subfield code count: Number of character positions used for a subfield code (2)
289 # Base address of data: 541
290 # Encoding level: Full level
291 # Descriptive cataloging form: ISBD punctuation included
292 # Multipart resource record level: Not specified or not applicable
293 # Length of the length-of-field portion: Number of characters in the length-of-field portion of a Directory entry (4)
294 # Length of the starting-character-position portion: Number of characters in the starting-character-position portion of a Directory entry (5)
295 # Length of the implementation-defined portion: Number of characters in the implementation-defined portion of a Directory entry (0)
296 # Undefined: Undefined
297  
298 =head1 EXAMPLE2
299  
300 =for comment filename=print_data_marc_leader_raw.pl
301  
302 use strict;
303 use warnings;
304  
305 use Data::MARC::Leader;
306 use MARC::Leader::Print;
307 use Unicode::UTF8 qw(encode_utf8);
308  
309 # Print object.
310 my $print = MARC::Leader::Print->new(
311 'lang' => 'cs',
312 'mode_desc' => 0,
313 );
314  
315 # Data object.
316 my $data_marc_leader = Data::MARC::Leader->new(
317 'bibliographic_level' => 'm',
318 'char_coding_scheme' => 'a',
319 'data_base_addr' => 541,
320 'descriptive_cataloging_form' => 'i',
321 'encoding_level' => ' ',
322 'impl_def_portion_len' => '0',
323 'indicator_count' => '2',
324 'length' => 2200,
325 'length_of_field_portion_len' => '4',
326 'multipart_resource_record_level' => ' ',
327 'starting_char_pos_portion_len' => '5',
328 'status' => 'c',
329 'subfield_code_count' => '2',
330 'type' => 'e',
331 'type_of_control' => ' ',
332 'undefined' => '0',
333 );
334  
335 # Print to output.
336 print encode_utf8(scalar $print->print($data_marc_leader)), "\n";
337  
338 # Output:
339 # Délka záznamu: 2200
340 # Status záznamu: c
341 # Typ záznamu: e
342 # Bibliografická úroveň: m
343 # Typ kontroly:
344 # Použitá znaková sada: a
345 # Délka indikátorů: 2
346 # Délka označení podpole: 2
347 # Bázová adresa údajů: 541
348 # Úroveň úplnosti záznamu:
349 # Forma katalogizačního popisu: i
350 # Úroveň záznamu vícedílného zdroje:
351 # Počet znaků délky pole: 4
352 # Délka počáteční znakové pozice: 5
353 # Délka implementačně definované části: 0
354 # Není definován: 0
355  
356 =head1 DEPENDENCIES
357  
358 L<Class::Utils>,
359 L<English>,
360 L<Error::Pure>.
361 L<MARC::Leader::L10N>,
362 L<Mo::utils::Language>.
363  
364 And optional L<Term::ANSIColor> for ANSI color support.
365  
366 =head1 SEE ALSO
367  
368 =over
369  
370 =item L<Data::MARC::Leader>
371  
372 Data object for MARC leader.
373  
374 =back
375  
376 =head1 REPOSITORY
377  
378 L<https://github.com/michal-josef-spacek/MARC-Leader-Print>
379  
380 =head1 AUTHOR
381  
382 Michal Josef Špaček L<mailto:skim@cpan.org>
383  
384 L<http://skim.cz>
385  
386 =head1 LICENSE AND COPYRIGHT
387  
388 © 2023-2026 Michal Josef Špaček
389  
390 BSD 2-Clause License
391  
392 =head1 VERSION
393  
394 0.08
395  
396 =cut
↑ Summary