From ab8e69f847bcb71e66f521a49739b4093819a715 Mon Sep 17 00:00:00 2001 From: Gerhard Gonter <ggonter@gmail.com> Date: Mon, 2 Sep 2019 15:33:56 +0200 Subject: [PATCH] do not depend on File::Sluper --- modules/util/Util/JSON.pm | 61 +++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/modules/util/Util/JSON.pm b/modules/util/Util/JSON.pm index 60a7d04..bf8cbb0 100755 --- a/modules/util/Util/JSON.pm +++ b/modules/util/Util/JSON.pm @@ -3,7 +3,22 @@ package Util::JSON; use strict; -use File::Slurper qw(read_text write_text); +BEGIN { + eval { + require File::Slurper; + + # *read_text= File::Slurper::read_text; + # *write_text= File::Slurper::write_text; + }; + + if ($@) + { + print STDERR "defining read_text ourselves\n"; + *read_text= *x_read_text; + *write_text= *x_wirte_text; + } +} + use JSON -convert_blessed_universally; sub read_json_file @@ -11,26 +26,17 @@ sub read_json_file my $fnm= shift; # BEGIN load JSON data from file content - local $/; # print "reading config [$fnm]\n"; -=begin comment - - open( my $fh, '<:utf8', $fnm ) or return undef; - my $json_text = <$fh>; - close ($fh); # decode_json( $json_text ); # for some reason, decode_json() barfs when otherwise cleanly read wide characters are present -=end comment -=cut - my $json_text; eval { $json_text= read_text($fnm); }; - if ($@) + if ($@ || !defined ($json_text)) { return undef; } @@ -49,16 +55,6 @@ sub write_json_file my $json= new JSON; my $json_str= $json->allow_blessed->convert_blessed->encode($x); -=begin comment - - open (J, '>:utf8', $json_fnm) or die ("can not write to [$json_fnm]"); - syswrite (J, $json_str); - close (J); - - -=end comment -=cut - write_text($json_fnm, $json_str); 1; @@ -75,6 +71,29 @@ sub get_config_item my $cfg= shift; } +sub x_read_text +{ + my $fnm= shift; + + open( my $fh, '<:utf8', $fnm ) or return undef; + local $/; + my $json_text = <$fh>; + close ($fh); + + $json_text; +} + +sub x_write_text +{ + my $json_fnm= shift; + my $json_str= shift; + + open (J, '>:utf8', $json_fnm) or die ("can not write to [$json_fnm]"); + syswrite (J, $json_str); + close (J); +} + + 1; __END__ -- GitLab