/
usr
/
local
/
lp
/
apps
/
malre
/
scripts
/
File Upload :
llllll
Current File: //usr/local/lp/apps/malre/scripts/json_io.pm
use warnings; use strict; use JSON; my $f_json; my $fh_json; sub fn_read_json { ### Opens a file containing .json data and converts it into a reference document ### $_[0] is the name of the .json file that we're reading from if ( ! defined $_[0] ) { return; } $f_json = $_[0]; my $ref_json; $fh_json = fn_get_lock($f_json); seek( $fh_json, 0, 0 ); my @temp = <$fh_json>; my $v_json = join( '', @temp ); if ( ! $v_json ) { return; } ### Test if the contents are json; if so, return them do { eval { decode_json( $v_json ); 1; } and do { $ref_json = decode_json( $v_json ); return $ref_json; } ### Otherwise just return } or do { print STDERR "File \"$f_json\" exists, but does not contain syntactically correct json data.\n"; } } sub fn_write_json { ### converts a reference object to json and writes it to an already open file ### $_[0] is the data object if ( ! defined $_[0] ) { return; } my $ref_json = $_[0]; if ( $ref_json ) { my $v_json = encode_json( $ref_json ); open( my $fh, '>', $f_json) or die "Cannot open " . $f_json . ": $!"; close( $fh ); seek( $fh_json, 0, 0 ); print $fh_json $v_json; } fn_release_lock($f_json); undef $fh_json; return; } 1;
Copyright ©2k19 -
Hexid
|
Tex7ure