blib/lib/MARC/Leader/Print.pm

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