MARC-Record
view release on metacpan or search on metacpan
lib/MARC/Field.pm view on Meta::CPAN
=head2 is_control_field()
Tells whether this field is one of the control tags from 001-009.
=cut
sub is_control_field {
my $self = shift;
return $self->{_is_control_field};
}
=head2 subfield(code)
When called in a scalar context returns the text from the first subfield
matching the subfield code.
my $subfield = $field->subfield( 'a' );
Or if you think there might be more than one you can get all of them by
calling in a list context:
my @subfields = $field->subfield( 'a' );
If no matching subfields are found, C<undef> is returned in a scalar context
and an empty list in a list context.
If the tag is a control field, C<undef> is returned and
C<$MARC::Field::ERROR> is set.
=cut
sub subfield {
my $self = shift;
my $code_wanted = shift;
croak( "Control fields (generally, just tags below 010) do not have subfields, use data()" )
if $self->is_control_field;
my @data = @{$self->{_subfields}};
my @found;
while ( defined( my $code = shift @data ) ) {
if ( $code eq $code_wanted ) {
push( @found, shift @data );
} else {
shift @data;
}
}
if ( wantarray() ) { return @found; }
return( $found[0] );
}
=head2 subfields()
Returns all the subfields in the field. What's returned is a list of
list refs, where the inner list is a subfield code and the subfield data.
For example, this might be the subfields from a 245 field:
(
[ 'a', 'Perl in a nutshell :' ],
[ 'b', 'A desktop quick reference.' ],
)
=cut
sub subfields {
my $self = shift;
if ($self->is_control_field) {
$self->_warn( "Control fields (generally, just tags below 010) do not have subfields" );
return;
}
my @list;
my @data = @{$self->{_subfields}};
while ( defined( my $code = shift @data ) ) {
push( @list, [$code, shift @data] );
}
return @list;
}
=head2 data()
Returns the data part of the field, if the tag number is less than 10.
=cut
sub data {
my $self = shift;
croak( "data() is only for control fields (generally, just tags below 010) , use subfield()" )
unless $self->is_control_field;
$self->{_data} = $_[0] if @_;
return $self->{_data};
}
=head2 add_subfields(code,text[,code,text ...])
Adds subfields to the end of the subfield list.
$field->add_subfields( 'c' => '1985' );
Returns the number of subfields added, or C<undef> if there was an error.
=cut
sub add_subfields {
my $self = shift;
croak( "Subfields are only for data fields (generally, just tags >= 010)" )
if $self->is_control_field;
push( @{$self->{_subfields}}, @_ );
return @_/2;
}
=head2 delete_subfield()
delete_subfield() allows you to remove subfields from a field:
( run in 1.313 second using v1.01-cache-2.11-cpan-ceb78f64989 )