File Coverage

blib/lib/MARC/Leader/Print.pm
Criterion Covered Total %
statement 30 71 42.2
branch 4 18 22.2
condition n/a
subroutine 7 10 70.0
pod 2 2 100.0
total 43 101 42.5


line stmt bran cond sub pod time code
1             package MARC::Leader::Print;
2              
3 3     3   202281 use strict;
  3         7  
  3         144  
4 3     3   17 use warnings;
  3         31  
  3         214  
5              
6 3     3   4450 use Class::Utils qw(set_params);
  3         55429  
  3         69  
7 3     3   2042 use Data::MARC::Leader::Utils;
  3         6646  
  3         137  
8 3     3   19 use English;
  3         9  
  3         16  
9 3     3   2585 use Error::Pure qw(err);
  3         6  
  3         2554  
10              
11             our $VERSION = 0.04;
12              
13             # Constructor.
14             sub new {
15 1     1 1 333113 my ($class, @params) = @_;
16              
17             # Create object.
18 1         4 my $self = bless {}, $class;
19              
20             # Use with ANSI sequences.
21 1         12 $self->{'mode_ansi'} = undef;
22              
23             # Use description.
24 1         5 $self->{'mode_desc'} = 1;
25              
26             # Output separator.
27 1         3 $self->{'output_separator'} = "\n";
28              
29             # Process parameters.
30 1         7 set_params($self, @params);
31              
32 1         20 $self->{'_utils'} = Data::MARC::Leader::Utils->new;
33              
34 1 50       2374 if (! defined $self->{'mode_ansi'}) {
35 1 50       8 if (exists $ENV{'NO_COLOR'}) {
    50          
36 0         0 $self->{'mode_ansi'} = 0;
37             } elsif (defined $ENV{'COLOR'}) {
38 0         0 $self->{'mode_ansi'} = 1;
39             } else {
40 1         4 $self->{'mode_ansi'} = 0;
41             }
42             }
43              
44             # Check routine for ANSI colors output.
45 1 50       4 if ($self->{'mode_ansi'}) {
46 0         0 eval {
47 0         0 require Term::ANSIColor;
48             };
49 0 0       0 if ($EVAL_ERROR) {
50 0         0 err "Cannot load 'Term::ANSIColor' module.";
51             }
52             }
53              
54 1         4 return $self;
55             }
56              
57             sub print {
58 0     0 1   my ($self, $leader_obj) = @_;
59              
60 0           my @ret;
61 0           push @ret, $self->_key('Record length').$leader_obj->length;
62 0           push @ret, $self->_key('Record status').$self->_print($leader_obj, 'status');
63 0           push @ret, $self->_key('Type of record').$self->_print($leader_obj, 'type');
64 0           push @ret, $self->_key('Bibliographic level').$self->_print($leader_obj,
65             'bibliographic_level');
66 0           push @ret, $self->_key('Type of control').$self->_print($leader_obj,
67             'type_of_control');
68 0           push @ret, $self->_key('Character coding scheme').$self->_print($leader_obj,
69             'char_coding_scheme');
70 0           push @ret, $self->_key('Indicator count').$self->_print($leader_obj,
71             'indicator_count');
72 0           push @ret, $self->_key('Subfield code count').$self->_print($leader_obj,
73             'subfield_code_count', 1);
74 0           push @ret, $self->_key('Base address of data').$leader_obj->data_base_addr;
75 0           push @ret, $self->_key('Encoding level').$self->_print($leader_obj,
76             'encoding_level');
77 0           push @ret, $self->_key('Descriptive cataloging form').$self->_print($leader_obj,
78             'descriptive_cataloging_form');
79 0           push @ret, $self->_key('Multipart resource record level').$self->_print($leader_obj,
80             'multipart_resource_record_level');
81 0           push @ret, $self->_key('Length of the length-of-field portion').$self->_print($leader_obj,
82             'length_of_field_portion_len', 1);
83 0           push @ret, $self->_key('Length of the starting-character-position portion').
84             $self->_print($leader_obj, 'starting_char_pos_portion_len', 1);
85 0           push @ret, $self->_key('Length of the implementation-defined portion').
86             $self->_print($leader_obj, 'impl_def_portion_len', 1);
87 0           push @ret, $self->_key('Undefined').$self->_print($leader_obj, 'undefined');
88              
89 0 0         return wantarray ? @ret : (join $self->{'output_separator'}, @ret);
90             }
91              
92             sub _key {
93 0     0     my ($self, $key) = @_;
94              
95 0           my $ret;
96 0 0         if ($self->{'mode_ansi'}) {
97 0           $ret = Term::ANSIColor::color('cyan').$key.Term::ANSIColor::color('reset');
98             } else {
99 0           $ret = $key;
100             }
101 0           $ret .= ': ';
102              
103 0           return $ret;
104             }
105              
106             sub _print {
107 0     0     my ($self, $leader_obj, $method_name, $value) = @_;
108              
109 0           my $ret_value = $leader_obj->$method_name;
110 0           my $ret;
111 0 0         if ($self->{'mode_desc'}) {
112 0           $ret = eval "\$self->{'_utils'}->desc_$method_name(\$ret_value);";
113 0 0         if (defined $value) {
114 0           $ret .= ' ('.$ret_value.')';
115             }
116             } else {
117 0           $ret = $ret_value;
118             }
119              
120 0           return $ret;
121             }
122              
123             1;
124              
125             __END__
126              
127             =pod
128              
129             =encoding utf8
130              
131             =head1 NAME
132              
133             MARC::Leader::Print - MARC leader class for print.
134              
135             =head1 SYNOPSIS
136              
137             use MARC::Leader::Print;
138              
139             my $obj = MARC::Leader::Print->new(%params);
140             my @ret = $obj->print($leader_obj);
141             my $ret = $obj->print($leader_obj);
142              
143             =head1 METHODS
144              
145             =head2 C<new>
146              
147             my $obj = MARC::Leader->new(%params);
148              
149             Constructor.
150              
151             =over 8
152              
153             =item * C<mode_ansi>
154              
155             Mode for ANSI color support:
156              
157             1 - ANSI color support enabled.
158             0 - ANSI color support disabled.
159              
160             When is undefined, env variables C<COLOR> or C<NO_COLOR> could control ANSI
161             color support.
162              
163             Default value is undef.
164              
165             =item * C<mode_desc>
166              
167             Use description instead of raw leader values.
168              
169             Default value is 1.
170              
171             =item * C<output_separator>
172              
173             Output separator used in scalar context of C<print()> method.
174              
175             Default value is "\n".
176              
177             =back
178              
179             Returns instance of object.
180              
181             =head2 C<print>
182              
183             my @ret = $obj->print($leader_obj);
184             my $ret = $obj->print($leader_obj);
185              
186             Process L<Data::MARC::Leader> instance to output print.
187             In scalar context compose printing output as one string.
188             In array context compose list of printing lines.
189              
190             Color (ANSI colors) output is controlled by 'mode_ansi' parameter
191             or env variables C<COLOR> and C<NO_COLOR>.
192              
193             Returns string in scalar context.
194             Returns array of string in array context.
195              
196             =head1 ERRORS
197              
198             new():
199             From Class::Utils::set_params():
200             Unknown parameter '%s'.
201              
202             =head1 EXAMPLE1
203              
204             =for comment filename=print_data_marc_leader.pl
205              
206             use strict;
207             use warnings;
208              
209             use Data::MARC::Leader;
210             use MARC::Leader::Print;
211              
212             # Print object.
213             my $print = MARC::Leader::Print->new;
214              
215             # Data object.
216             my $data_marc_leader = Data::MARC::Leader->new(
217             'bibliographic_level' => 'm',
218             'char_coding_scheme' => 'a',
219             'data_base_addr' => 541,
220             'descriptive_cataloging_form' => 'i',
221             'encoding_level' => ' ',
222             'impl_def_portion_len' => '0',
223             'indicator_count' => '2',
224             'length' => 2200,
225             'length_of_field_portion_len' => '4',
226             'multipart_resource_record_level' => ' ',
227             'starting_char_pos_portion_len' => '5',
228             'status' => 'c',
229             'subfield_code_count' => '2',
230             'type' => 'e',
231             'type_of_control' => ' ',
232             'undefined' => '0',
233             );
234              
235             # Print to output.
236             print scalar $print->print($data_marc_leader), "\n";
237              
238             # Output:
239             # Record length: 2200
240             # Record status: Corrected or revised
241             # Type of record: Cartographic material
242             # Bibliographic level: Monograph/Item
243             # Type of control: No specified type
244             # Character coding scheme: UCS/Unicode
245             # Indicator count: Number of character positions used for indicators
246             # Subfield code count: Number of character positions used for a subfield code (2)
247             # Base address of data: 541
248             # Encoding level: Full level
249             # Descriptive cataloging form: ISBD punctuation included
250             # Multipart resource record level: Not specified or not applicable
251             # Length of the length-of-field portion: Number of characters in the length-of-field portion of a Directory entry (4)
252             # Length of the starting-character-position portion: Number of characters in the starting-character-position portion of a Directory entry (5)
253             # Length of the implementation-defined portion: Number of characters in the implementation-defined portion of a Directory entry (0)
254             # Undefined: Undefined
255              
256             =head1 EXAMPLE2
257              
258             =for comment filename=print_data_marc_leader_raw.pl
259              
260             use strict;
261             use warnings;
262              
263             use Data::MARC::Leader;
264             use MARC::Leader::Print;
265              
266             # Print object.
267             my $print = MARC::Leader::Print->new(
268             'mode_desc' => 0,
269             );
270              
271             # Data object.
272             my $data_marc_leader = Data::MARC::Leader->new(
273             'bibliographic_level' => 'm',
274             'char_coding_scheme' => 'a',
275             'data_base_addr' => 541,
276             'descriptive_cataloging_form' => 'i',
277             'encoding_level' => ' ',
278             'impl_def_portion_len' => '0',
279             'indicator_count' => '2',
280             'length' => 2200,
281             'length_of_field_portion_len' => '4',
282             'multipart_resource_record_level' => ' ',
283             'starting_char_pos_portion_len' => '5',
284             'status' => 'c',
285             'subfield_code_count' => '2',
286             'type' => 'e',
287             'type_of_control' => ' ',
288             'undefined' => '0',
289             );
290              
291             # Print to output.
292             print scalar $print->print($data_marc_leader), "\n";
293              
294             # Output:
295             # Record length: 2200
296             # Record status: c
297             # Type of record: e
298             # Bibliographic level: m
299             # Type of control:
300             # Character coding scheme: a
301             # Indicator count: 2
302             # Subfield code count: 2
303             # Base address of data: 541
304             # Encoding level:
305             # Descriptive cataloging form: i
306             # Multipart resource record level:
307             # Length of the length-of-field portion: 4
308             # Length of the starting-character-position portion: 5
309             # Length of the implementation-defined portion: 0
310             # Undefined: 0
311              
312             =head1 DEPENDENCIES
313              
314             L<Class::Utils>,
315             L<Data::MARC::Leader::Utils>,
316             L<English>,
317             L<Error::Pure>.
318              
319             And optional L<Term::ANSIColor> for ANSI color support.
320              
321             =head1 SEE ALSO
322              
323             =over
324              
325             =item L<Data::MARC::Leader>
326              
327             Data object for MARC leader.
328              
329             =back
330              
331             =head1 REPOSITORY
332              
333             L<https://github.com/michal-josef-spacek/MARC-Leader-Print>
334              
335             =head1 AUTHOR
336              
337             Michal Josef Špaček L<mailto:skim@cpan.org>
338              
339             L<http://skim.cz>
340              
341             =head1 LICENSE AND COPYRIGHT
342              
343             © 2023-2024 Michal Josef Špaček
344              
345             BSD 2-Clause License
346              
347             =head1 VERSION
348              
349             0.04
350              
351             =cut