From 0480969bfac140a0d0a68ac098302285d39a80bf Mon Sep 17 00:00:00 2001
From: Andreas Romeyke <art1@andreas-romeyke.de>
Date: Mon, 25 Oct 2021 15:52:41 +0200
Subject: [PATCH] - removed superflous global variable - refactoring, replaced
 write_file() with path()->spew_utf8()

---
 lib/SLUB/LZA/SIPBuilderBagIt.pm | 25 +++----------------------
 1 file changed, 3 insertions(+), 22 deletions(-)

diff --git a/lib/SLUB/LZA/SIPBuilderBagIt.pm b/lib/SLUB/LZA/SIPBuilderBagIt.pm
index 309aa80..9cb62f7 100644
--- a/lib/SLUB/LZA/SIPBuilderBagIt.pm
+++ b/lib/SLUB/LZA/SIPBuilderBagIt.pm
@@ -60,9 +60,7 @@ use Term::ANSIColor;         # colored print
 use XML::LibXML;
 use XML::XPath;
 
-our $VERBOSE = 0;            # print output switch 0 => on, 1 and larger = off
 our $with_debug = 0;         # output debug infos and files
-
 # catalogue infos
 my $swb_url        = 'https://sru.bsz-bw.de/swb';
 my $searchkey_swb  = 'pica.swn';
@@ -101,7 +99,6 @@ sub get_marc21_text_node($marc21, $mapping) {
     $xpath .= "text()";
     # HINT: xpath example //*[@tag='245']/*[local-name()='subfield' and @code='a']/text()
     my $text = XML::XPath->new($marc21)->findnodes_as_string($xpath);
-
     return $text;
 }
 
@@ -109,7 +106,6 @@ sub get_marc21_from_catalogue($ppn){
     if(! $_[0]){ croak "ppn not defined!"; }
     my $marc21;
     my $use_fallback;
-
     eval { # try SWB catalogue
         $marc21 = SLUB::LZA::SIPBuilderBagIt::get_marc21_from($swb_url, $ppn, $searchkey_swb, $recordschema_swb);
     };
@@ -123,7 +119,6 @@ sub get_marc21_from_catalogue($ppn){
             die "$@"; # throw exception again
         }
     }
-
     if ($use_fallback) {
         eval { # try K10plus catalogue
             $marc21 = SLUB::LZA::SIPBuilderBagIt::get_marc21_from($swb_url, $ppn, $searchkey_k10p, $recordschema_k10p);
@@ -139,7 +134,6 @@ sub get_marc21_from_catalogue($ppn){
             }
         }
     }
-
     print_scalar_data( "INFO: Found metadata for PPN '$ppn'", "white");
     return $marc21;
 }
@@ -167,7 +161,7 @@ sub get_marc21_from($url, $ppn,$key,$schema){
         my $parser = XML::LibXML->new();
         if ($with_debug) {
             say "write DEBUG_${ppn}_catalog_response.xml";
-            SLUB::LZA::SIPBuilderBagIt::write_file("DEBUG_${ppn}_catalog_response.xml", $response->decoded_content);
+            path("DEBUG_${ppn}_catalog_response.xml")->spew_utf8($response->decoded_content);
         }
         my $recordData = $xp->findnodes_as_string('/*[local-name()="searchRetrieveResponse"]/*[local-name()="records"]/*[local-name()="record"]/*[local-name()="recordData"]/*');
         if (!$recordData) { croak("ERROR: Did not get any <recordData/> for PPN '$ppn' using '$sru'");}
@@ -175,7 +169,7 @@ sub get_marc21_from($url, $ppn,$key,$schema){
         add_marc21_schema_location($marc_doc);
         if ($with_debug) {
             say "write DEBUG_${ppn}_extracted_marc_record.xml";
-            SLUB::LZA::SIPBuilderBagIt::write_file("DEBUG_${ppn}_extracted_marc_record.xml", $marc_doc);
+            path("DEBUG_${ppn}_extracted_marc_record.xml")->spew_utf8($marc_doc);
         }
         return $marc_doc->serialize();
     }
@@ -195,14 +189,6 @@ sub add_marc21_schema_location($marc_doc) {
     return 1;
 }
 
-sub write_file($filename, $value){
-    # write data to file (UTF-8)
-    open(my $fh, '>:encoding(UTF-8)', $filename) || (croak "Can't open '$filename', $!");
-    print $fh $value;
-    close($fh) || (croak "could not close file '$filename', $!");
-    return 1;
-}
-
 sub generateBagName($file_date, $ppn, $noppn){
     $file_date =~ s/T/_/g; # replace 'T' with '_'
     $file_date =~ s/:/-/g; # replace ':' with '-'
@@ -247,12 +233,10 @@ sub copyFilesToMeta($ppn, $metaPath,$rightsFilePath,$aRefAddMetaFile,$marc21){
             }
         }
     }
-
     # marc21.xml
     if ($ppn && $marc21) {
-        write_file("$metaPath/marc21.xml", $marc21);
+        path("$metaPath/marc21.xml")->spew_utf8($marc21);
     }
-
     # rights.xml
     if(basename($rightsFilePath) ne "rights.xml"){
         my $rights_name = basename($rightsFilePath);
@@ -263,7 +247,6 @@ sub copyFilesToMeta($ppn, $metaPath,$rightsFilePath,$aRefAddMetaFile,$marc21){
     }else{
         copy($rightsFilePath, $metaPath) or die "Copy failed: $!";
     }
-
     return 1;
 }
 
@@ -282,7 +265,6 @@ sub validateRightsXML($rightsFilePathString,$xsdName){
     my $schema = XML::LibXML::Schema->new(location => $xsd_file);
     my $parser = XML::LibXML->new;
     my $doc    = $parser->parse_file($rightsFilePath);
-
     eval {
         $schema->validate($doc);
     };
@@ -293,7 +275,6 @@ sub validateRightsXML($rightsFilePathString,$xsdName){
 }
 
 sub print_scalar_data($p,$pColor){
-    # paragraph
     if($p ne "") {
         printf "%" . length($p) . "s\n", colored($p, "bold $pColor");
     }
-- 
GitLab