Bug 31535: Fix warning - uninitialized value $mode in string ne (addbiblio.pl)
[srvgit] / Koha / SimpleMARC.pm
index cb92bff..6fd08b1 100644 (file)
@@ -2,33 +2,39 @@ package Koha::SimpleMARC;
 
 # Copyright 2009 Kyle M. Hall <kyle.m.hall@gmail.com>
 
-use Modern::Perl;
-
-#use MARC::Record;
-
-require Exporter;
-
-our @ISA = qw(Exporter);
-our %EXPORT_TAGS = ( 'all' => [ qw(
-
-) ] );
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
-our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
-
-our @EXPORT = qw(
-  read_field
-  add_field
-  update_field
-  copy_field
-  copy_and_replace_field
-  move_field
-  delete_field
-  field_exists
-  field_equals
-);
 
+use Modern::Perl;
 
-our $debug = 0;
+our (@ISA, @EXPORT_OK);
+BEGIN {
+    require Exporter;
+    our @ISA = qw(Exporter);
+
+    @EXPORT_OK = qw(
+      read_field
+      add_field
+      update_field
+      copy_field
+      copy_and_replace_field
+      move_field
+      delete_field
+      field_exists
+      field_equals
+    );
+}
 
 =head1 NAME
 
@@ -127,8 +133,8 @@ sub copy_and_replace_field {
     if ( ! ( $record && $fromFieldName && $toFieldName ) ) { return; }
 
 
-    if ( not $fromSubfieldName or $fromSubfieldName eq ''
-      or not $toSubfieldName or $toSubfieldName eq ''
+    if ( !defined $fromSubfieldName or $fromSubfieldName eq ''
+      or !defined $toSubfieldName or $toSubfieldName eq ''
     ) {
         _copy_move_field(
             {   record        => $record,
@@ -466,9 +472,9 @@ sub move_field {
     my $regex = $params->{regex};
     my $field_numbers = $params->{field_numbers} // [];
 
-    if (   not $fromSubfieldName
+    if (   !defined $fromSubfieldName
         or $fromSubfieldName eq ''
-        or not $toSubfieldName
+        or !defined $toSubfieldName
         or $toSubfieldName eq '' ) {
         _copy_move_field(
             {   record        => $record,
@@ -512,7 +518,7 @@ sub delete_field {
     my $subfieldName = $params->{subfield};
     my $field_numbers = $params->{field_numbers} // [];
 
-    if ( not $subfieldName or $subfieldName eq '' ) {
+    if ( !defined $subfieldName or $subfieldName eq '' ) {
         _delete_field({ record => $record, field => $fieldName, field_numbers => $field_numbers });
     } else {
         _delete_subfield({ record => $record, field => $fieldName, subfield => $subfieldName, field_numbers => $field_numbers });
@@ -550,6 +556,7 @@ sub _delete_subfield {
 
     foreach my $field ( @fields ) {
         $field->delete_subfield( code => $subfieldName );
+        $record->delete_field( $field ) unless $field->subfields();
     }
 }
 
@@ -629,6 +636,9 @@ sub _modify_values {
     my $regex = $params->{regex};
 
     if ( $regex and $regex->{search} ) {
+        my $replace = $regex->{replace};
+        $replace =~ s/"/\\"/g;                    # Protection from embedded code
+        $replace = '"' . $replace . '"'; # Put in a string for /ee
         $regex->{modifiers} //= q||;
         my @available_modifiers = qw( i g );
         my $modifiers = q||;
@@ -638,16 +648,16 @@ sub _modify_values {
         }
         foreach my $value ( @$values ) {
             if ( $modifiers =~ m/^(ig|gi)$/ ) {
-                $value =~ s/$regex->{search}/$regex->{replace}/ig;
+                $value =~ s/$regex->{search}/$replace/igee;
             }
             elsif ( $modifiers eq 'i' ) {
-                $value =~ s/$regex->{search}/$regex->{replace}/i;
+                $value =~ s/$regex->{search}/$replace/iee;
             }
             elsif ( $modifiers eq 'g' ) {
-                $value =~ s/$regex->{search}/$regex->{replace}/g;
+                $value =~ s/$regex->{search}/$replace/gee;
             }
             else {
-                $value =~ s/$regex->{search}/$regex->{replace}/;
+                $value =~ s/$regex->{search}/$replace/ee;
             }
         }
     }