File | /home/tamil/util/marc-moose/lib/MARC/Moose/Record.pm |
Statements Executed | 1018 |
Total Time | 0.00230280000000001 seconds |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1000 | 1 | 1 | 3.54ms | 3.54ms | __ANON__[:20] | MARC::Moose::Record::
0 | 0 | 0 | 0s | 0s | BEGIN | MARC::Moose::Record::
0 | 0 | 0 | 0s | 0s | append | MARC::Moose::Record::
0 | 0 | 0 | 0s | 0s | check | MARC::Moose::Record::
0 | 0 | 0 | 0s | 0s | field | MARC::Moose::Record::
0 | 0 | 0 | 0s | 0s | set_leader_length | MARC::Moose::Record::
Line | Stmts. | Exclusive Time | Avg. | Code |
---|---|---|---|---|
1 | package MARC::Moose::Record; | |||
2 | # ABSTRACT: MARC::Moose bibliographic record | |||
3 | ||||
4 | 3 | 110µs | 37µs | use namespace::autoclean; # spent 46µs making 1 call to namespace::autoclean::import |
5 | 3 | 148µs | 49µs | use Moose; # spent 6.84ms making 1 call to Moose::Exporter::__ANON__[/usr/local/lib/perl/5.10.0/Moose/Exporter.pm:425] |
6 | ||||
7 | 3 | 403µs | 134µs | use Carp; # spent 69µs making 1 call to Exporter::import |
8 | ||||
9 | ||||
10 | 1 | 8µs | 8µs | has leader => ( # spent 4.22ms making 1 call to Moose::has |
11 | is => 'ro', | |||
12 | isa => 'Str', | |||
13 | writer => '_leader', | |||
14 | default => ' ' x 24, | |||
15 | ); | |||
16 | ||||
17 | has fields => ( | |||
18 | is => 'rw', | |||
19 | isa => 'ArrayRef', | |||
20 | 1000 | 1.28ms | 1µs | # spent 3.54ms within MARC::Moose::Record::__ANON__[/home/tamil/util/marc-moose/lib/MARC/Moose/Record.pm:20] which was called 1000 times, avg 4µs/call:
# 1000 times (3.54ms+0s) by Class::MOP::Method::Generated::__ANON__[generated method (unknown origin):63] or Class::MOP::Method::Generated::__ANON__[generated method (unknown origin):43] at line 34 of /home/tamil/util/marc-moose/generated method (unknown origin), avg 4µs/call |
21 | 1 | 8µs | 8µs | ); # spent 2.39ms making 1 call to Moose::has |
22 | ||||
23 | ||||
24 | sub set_leader_length { | |||
25 | my ($self, $length, $offset) = @_; | |||
26 | ||||
27 | carp "Record length of $length is larger than the MARC spec allows 99999" | |||
28 | if $length > 99999; | |||
29 | ||||
30 | my $leader = $self->leader; | |||
31 | substr($leader, 0, 5) = sprintf("%05d", $length); | |||
32 | substr($leader, 12, 5) = sprintf("%05d", $offset); | |||
33 | ||||
34 | # Default leader various pseudo variable fields | |||
35 | # Force UNICODE MARC21: substr($leader, 9, 1) = 'a'; | |||
36 | substr($leader, 10, 2) = '22'; | |||
37 | substr($leader, 20, 4) = '4500'; | |||
38 | ||||
39 | $self->_leader( $leader ); | |||
40 | } | |||
41 | ||||
42 | ||||
43 | sub append { | |||
44 | my ($self, $field_to_add) = @_; | |||
45 | ||||
46 | # Control field correctness | |||
47 | carp "Append a non MARC::Moose::Field" | |||
48 | unless ref($field_to_add) =~ /^MARC::Moose::Field/; | |||
49 | ||||
50 | my $tag_first = substr($field_to_add->tag, 0, 1); | |||
51 | my @sf; | |||
52 | for my $field ( @{$self->fields} ) { | |||
53 | if ( $field_to_add and substr($field->tag, 0, 1) gt $tag_first ) { | |||
54 | push @sf, $field_to_add; | |||
55 | $field_to_add = undef; | |||
56 | } | |||
57 | push @sf, $field; | |||
58 | } | |||
59 | push @sf, $field_to_add if $field_to_add; | |||
60 | $self->fields( \@sf ); | |||
61 | } | |||
62 | ||||
63 | ||||
64 | 1 | 400ns | 400ns | my %_field_regex; |
65 | ||||
66 | sub field { | |||
67 | my $self = shift; | |||
68 | my @specs = @_; | |||
69 | ||||
70 | my @list; | |||
71 | 3 | 274µs | 91µs | use YAML; # spent 44µs making 1 call to Exporter::import |
72 | for my $tag ( @specs ) { | |||
73 | my $regex = $_field_regex{ $tag }; | |||
74 | # Compile & stash it if necessary | |||
75 | unless ( $regex ) { | |||
76 | $regex = qr/^$tag$/; | |||
77 | $_field_regex{ $tag } = $regex; | |||
78 | } | |||
79 | for my $field ( @{$self->fields} ) { | |||
80 | if ( $field->tag =~ $regex ) { | |||
81 | return $field unless wantarray; | |||
82 | push @list, $field; | |||
83 | } | |||
84 | } | |||
85 | } | |||
86 | return @list; | |||
87 | } | |||
88 | ||||
89 | ||||
90 | sub check { | |||
91 | my $self = shift; | |||
92 | ||||
93 | for my $field ( @{$self->fields} ) { | |||
94 | for my $subf ( @{$field->subf} ) { | |||
95 | if ( @$subf != 2 ) { | |||
96 | print "NON !!!\n"; | |||
97 | exit; | |||
98 | } | |||
99 | } | |||
100 | } | |||
101 | } | |||
102 | ||||
103 | ||||
104 | 1 | 19µs | 19µs | __PACKAGE__->meta->make_immutable; # spent 30.9ms making 1 call to Class::MOP::Class::make_immutable
# spent 30µs making 1 call to MARC::Moose::Record::meta |
105 | ||||
106 | 1 | 33µs | 33µs | 1; |
107 | ||||
108 | ||||
109 | =head1 SYNOPSYS | |||
110 | ||||
111 | use MARC::Moose::Record; | |||
112 | use MARC::Moose::Field::Control; | |||
113 | use MARC::Moose::Field::Std; | |||
114 | use MARC::Moose::Formater::Text; | |||
115 | ||||
116 | my $record = MARC::Moose::Record->new( | |||
117 | fields => [ | |||
118 | MARC::Moose::Field::Control->new( | |||
119 | tag => '001', | |||
120 | value => '1234' ), | |||
121 | MARC::Moose::Field::Std->new( | |||
122 | tag => '245', | |||
123 | subf => [ [ a => 'MARC is dying for ever:' ], [ b => 'will it ever happen?' ] ] ), | |||
124 | MARC::Moose::Field::Std->new( | |||
125 | tag => '260', | |||
126 | subf => [ | |||
127 | [ a => 'Paris:' ], | |||
128 | [ b => 'Usefull Press,' ], | |||
129 | [ c => '2010.' ], | |||
130 | ] ), | |||
131 | MARC::Moose::Field::Std->new( | |||
132 | tag => '600', | |||
133 | subf => [ [ a => 'Library' ], [ b => 'Standards' ] ] ), | |||
134 | MARC::Moose::Field::Std->new( | |||
135 | tag => '900', | |||
136 | subf => [ [ a => 'My local field 1' ] ] ), | |||
137 | MARC::Moose::Field::Std->new( | |||
138 | tag => '901', | |||
139 | subf => [ [ a => 'My local field 1' ] ] ), | |||
140 | ] | |||
141 | ); | |||
142 | ||||
143 | my $formater = MARC::Moose::Formater::Text->new(); | |||
144 | print $formater->format( $record ); | |||
145 | ||||
146 | $record->fields( [ grep { $_->tag < 900 } @{$record->fields} ] ); | |||
147 | print "After local fields removing:\n", $formater->format($record); | |||
148 | ||||
149 | =head1 DESCRIPTION | |||
150 | ||||
151 | MARC::Moose::Record is an object, Moose based object, representing a MARC::Moose | |||
152 | bibliographic record. It can be a MARC21, UNIMARC, or whatever biblio record. | |||
153 | ||||
154 | =attr leader | |||
155 | ||||
156 | Read-only string. The leader is fixed by set_leader_length method. | |||
157 | ||||
158 | =attr fields | |||
159 | ||||
160 | ArrayRef on MARC::Moose::Field objects: MARC::Moose:Fields::Control and | |||
161 | MARC::Moose::Field::Std. | |||
162 | ||||
163 | =method append( I<field> ) | |||
164 | ||||
165 | Append a MARC::Moose::Field in the record. The record is appended at the end of | |||
166 | numerical section, ie if you append for example a 710 field, it will be placed | |||
167 | at the end of the 7xx fields section, just before 8xx section or at the end of | |||
168 | fields list. | |||
169 | ||||
170 | $record->append( | |||
171 | MARC::Moose::Field::Std->new( | |||
172 | tag => '100', | |||
173 | subf => [ [ a => 'Poe, Edgar Allan' ], | |||
174 | [ u => 'Translation' ] ] | |||
175 | ) ); | |||
176 | ||||
177 | =method field( I<tag> ) | |||
178 | ||||
179 | Returns a list of tags that match the field specifier, or an empty list if | |||
180 | nothing matched. In scalar context, returns the first matching tag, or undef | |||
181 | if nothing matched. | |||
182 | ||||
183 | The field specifier can be a simple number (i.e. "245"), or use the "." | |||
184 | notation of wildcarding (i.e. subject tags are "6.."). All fields are returned | |||
185 | if "..." is specified. | |||
186 | ||||
187 | =method set_leader_length( I<length>, I<offset> ) | |||
188 | ||||
189 | This method is called to reset leader length of record and offset of data | |||
190 | section. This means something only for ISO2709 formated records. So this method | |||
191 | is exlusively called by any formater which has to build a valid ISO2709 data | |||
192 | stream. It also forces leader position 10 and 20-23 since this variable values | |||
193 | aren't variable at all for any ordinary MARC record. | |||
194 | ||||
195 | Called by L<MARC::Moose::Formater::Iso2709>. | |||
196 | ||||
197 | $record->set_leader_length( $length, $offset ); | |||
198 | ||||
199 | =head1 SEE ALSO | |||
200 | ||||
201 | =for :list | |||
202 | * L<MARC::Moose> | |||
203 | 1 | 15µs | 15µs | * L<MARC::Moose::Field> |