DiaColloDB
view release on metacpan or search on metacpan
DiaColloDB/Utils.pm view on Meta::CPAN
binmode($fh,':raw');
local $/=undef;
my $buf = <$fh>;
close($fh) if (!ref($file));
return $that->loadJsonString(\$buf,@_);
}
##--------------------------------------------------------------
## JSON: save
## $str = PACKAGE::saveJsonString($data)
## $str = PACKAGE::saveJsonString($data,%opts)
## + %opts passed to JSON::to_json(), e.g. (pretty=>0, canonical=>0)'
## + supports $opts{json} = $json_obj
sub saveJsonString {
my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
my $data = shift;
my %opts = @_;
return $opts{json}->encode($data) if ($opts{json});
return to_json($data, {utf8=>1, allow_nonref=>1, allow_unknown=>1, allow_blessed=>1, convert_blessed=>1, pretty=>1, canonical=>1, %opts});
}
## $bool = PACKAGE::saveJsonFile($data,$filename_or_handle,%opts)
sub saveJsonFile {
my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
my $data = shift;
my $file = shift;
my $fh = ref($file) ? $file : IO::File->new(">$file");
$that->logconfess("saveJsonFile() failed to open file '$file': $!") if (!$fh);
binmode($fh,':raw');
$fh->print($that->saveJsonString($data,@_)) or return undef;
if (!ref($file)) { close($fh) || return undef; }
return 1;
}
##--------------------------------------------------------------
## JSON: object
## $json = jsonxs()
## $json = jsonxs(%opts)
## $json = jsonxs(\%opts)
sub jsonxs {
my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
my %opts = (
utf8=>1, relaxed=>1, allow_nonref=>1, allow_unknown=>1, allow_blessed=>1, convert_blessed=>1, pretty=>1, canonical=>1,
(@_==1 ? %{$_[0]} : @_),
);
my $jxs = JSON->new;
foreach (grep {$jxs->can($_)} keys %opts) {
$jxs->can($_)->($jxs,$opts{$_});
}
return $jxs;
}
BEGIN { *json = \&jsonxs; }
##==============================================================================
## Functions: env
## \%setenv = PACKAGE::env_set(%setenv)
sub env_set {
my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
my %setenv = @_;
my ($key,$val);
while (($key,$val)=each(%setenv)) {
if (!defined($val)) {
#$that->trace("ENV_UNSET $key");
delete($ENV{$key});
} else {
#$that->trace("ENV_SET $key=$val");
$ENV{$key} = $val;
}
}
return \%setenv;
}
## \%oldvals = PACKAGE::env_push(%setenv)
our @env_stack = qw();
sub env_push {
my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
my %setenv = @_;
my %oldenv = map {($_=>$ENV{$_})} keys %setenv;
push(@env_stack, \%oldenv);
$that->env_set(%setenv);
return \%oldenv;
}
## \%restored = PACKAGE::env_pop(%setenv)
sub env_pop {
my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
my $oldvals = pop(@env_stack);
$that->env_set(%$oldvals) if ($oldvals);
return $oldvals;
}
##==============================================================================
## Functions: run
## $rc = PACKAGE::runcmd($cmd)
## $rc = PACKAGE::runcmd(@cmd)
## + does log trace at level $TRACE_RUNCMD
sub runcmd {
my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
$that->trace("CMD ", join(' ',@_));
return system(@_);
}
## $fh_or_undef = PACKAGE::opencmd($cmd)
## $fh_or_undef = PACKAGE::opencmd($mode,@argv)
## + does log trace at level $TRACE_RUNCMD
sub opencmd {
my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
$that->trace("CMD ", join(' ',@_));
my $fh = IO::Handle->new();
if ($#_ > 0) {
open($fh,$_[0],$_[1],@_[2..$#_])
} else {
open($fh,$_[0]);
}
$that->logconfess("opencmd() failed for \`", join(' ',@_), "': $!") if (!$fh);
return $fh;
}
## $bool = crun(@IPC_Run_args)
## + wrapper for IPC::Run::run(@IPC_Run_args) with $ENV{LC_ALL}='C'
sub crun {
my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
$that->trace("RUN ", join(' ',
map {
(ref($_)
? (ref($_) eq 'ARRAY'
? join(' ', @$_)
: ref($_))
: $_)
} @_));
$that->env_push(LC_ALL=>'C');
my $rc = IPC::Run::run(@_);
$that->env_pop();
return $rc;
}
## $cmd_prefix = sortCmd($njobs=$DiaColloDB::NJOBS)
## + returns command-prefix for UNIX sort
## + uses environment variable $DIACOLLO_SORT if present, otherwise $SORT if set
## + defualts to system 'sort' command from PATH with appended sortJobs() options
sub sortCmd {
my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
( run in 0.529 second using v1.01-cache-2.11-cpan-39bf76dae61 )