From 1cd0642ff92e7376d696c69f39605c1682a2a1dc Mon Sep 17 00:00:00 2001
From: Robin Sheat <robin@catalyst.net.nz>
Date: Fri, 12 Oct 2012 14:24:58 +1300
Subject: [PATCH] Add the 'settag' function to MARC::Field to allow tags to be
 renamed

---
 marc-record/lib/MARC/Field.pm |   16 ++++++++++++++++
 marc-record/t/rename-field.t  |   22 ++++++++++++++++++++++
 2 files changed, 38 insertions(+)
 create mode 100644 marc-record/t/rename-field.t

diff --git a/marc-record/lib/MARC/Field.pm b/marc-record/lib/MARC/Field.pm
index a4b2fa7..42bb3d7 100644
--- a/marc-record/lib/MARC/Field.pm
+++ b/marc-record/lib/MARC/Field.pm
@@ -125,6 +125,22 @@ sub tag {
     return $self->{_tag};
 }
 
+=head2 settag(tag)
+
+Changes the tag number of this field. Updates the control status accordingly.
+Will C<croak> if an invalid value is passed in.
+
+=cut
+
+sub settag {
+    my ( $self, $tagno ) = @_;
+
+    ( $tagno =~ /^[0-9A-Za-z]{3}$/ )
+      or croak("Tag \"$tagno\" is not a valid tag.");
+    $self->{_tag}              = $tagno;
+    $self->{_is_control_field} = is_controlfield_tag($tagno);
+}
+
 =head2 indicator(indno)
 
 Returns the specified indicator.  Returns C<undef> and sets
diff --git a/marc-record/t/rename-field.t b/marc-record/t/rename-field.t
new file mode 100644
index 0000000..11611ef
--- /dev/null
+++ b/marc-record/t/rename-field.t
@@ -0,0 +1,22 @@
+use strict;
+use warnings;
+use Test::More tests => 3;
+use MARC::Record;
+
+my $record = MARC::Record->new();
+$record->append_fields(MARC::Field->new('035', '', '', 'a' => 'Foo'));
+$record->append_fields(MARC::Field->new('035', '', '', 'a' => 'Bar'));
+$record->append_fields(MARC::Field->new('035', '', '', 'a' => 'Baz'));
+
+
+my @original_035s = $record->field('035');
+is scalar(@original_035s), 3, 'found 3 035 fields';
+
+$original_035s[0]->settag('100');
+
+my @new_100 = $record->field('100');
+is scalar(@new_100), 1, 'found 1 new 100 field';
+
+@original_035s = $record->field('035');
+is scalar(@original_035s), 2, 'found 2 035 fields';
+
-- 
1.7.9.5

