Pin versions in cpanfile, fix coding style and cleanup a bit
This commit is contained in:
@@ -1,41 +1,50 @@
|
||||
package Slackmetarep::Conf;
|
||||
# loads config
|
||||
# Loads json formatted config
|
||||
|
||||
use 5.018;
|
||||
use strict;
|
||||
use warnings;
|
||||
use utf8;
|
||||
use open qw(:std :utf8);
|
||||
use English qw( -no_match_vars );
|
||||
|
||||
use vars qw/$VERSION/;
|
||||
use open qw (:std :utf8);
|
||||
use English qw ( -no_match_vars );
|
||||
use Encode qw (encode);
|
||||
use JSON::XS;
|
||||
|
||||
use Exporter qw(import);
|
||||
our @EXPORT_OK = qw(loadConf);
|
||||
use version; our $VERSION = qw (1.0);
|
||||
use Exporter qw (import);
|
||||
our @EXPORT_OK = qw (LoadConf);
|
||||
|
||||
$VERSION = '1.0';
|
||||
sub LoadConf {
|
||||
my $file = 'data/config.json';
|
||||
open my $FILEHANDLE, '<', $file || die "[FATA] No conf at $file: $OS_ERROR\n";
|
||||
|
||||
sub loadConf {
|
||||
my $c = 'data/config.json';
|
||||
open my $CH, '<', $c || die "[FATA] No conf at $c: $OS_ERROR\n";
|
||||
my $len = (stat ($c)) [7];
|
||||
# Use binmode in order to get amount of read bytes (not chars!) from read()
|
||||
binmode $FILEHANDLE;
|
||||
my $len = (stat $file) [7];
|
||||
my $json;
|
||||
my $readlen = read $CH, $json, $len;
|
||||
my $readlen = read $FILEHANDLE, $json, $len;
|
||||
$json = encode 'UTF-8', $json;
|
||||
|
||||
unless ($readlen) {
|
||||
close $CH; ## no critic (InputOutput::RequireCheckedSyscalls
|
||||
die "[FATA] Unable to read $c: $OS_ERROR\n";
|
||||
unless (defined $readlen) {
|
||||
close $FILEHANDLE; ## no critic (InputOutput::RequireCheckedSyscalls
|
||||
die "[FATA] Unable to read $file: $OS_ERROR\n";
|
||||
}
|
||||
|
||||
if ($readlen != $len) {
|
||||
close $CH; ## no critic (InputOutput::RequireCheckedSyscalls
|
||||
die "[FATA] File $c is $len bytes on disk, but we read only $readlen bytes\n";
|
||||
close $FILEHANDLE; ## no critic (InputOutput::RequireCheckedSyscalls
|
||||
die "[FATA] File $file is $len bytes on disk, but we read only $readlen bytes\n";
|
||||
}
|
||||
|
||||
close $CH; ## no critic (InputOutput::RequireCheckedSyscalls
|
||||
close $FILEHANDLE; ## no critic (InputOutput::RequireCheckedSyscalls
|
||||
|
||||
my $j = JSON::XS->new->utf8->relaxed;
|
||||
return $j->decode ($json);
|
||||
my $config = eval { $j->decode ($json); };
|
||||
|
||||
unless (defined $config) {
|
||||
die "[FATA] File $file does not contain a valid json data: $EVAL_ERROR\n";
|
||||
}
|
||||
|
||||
return $config;
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
Reference in New Issue
Block a user