File Coverage

lib/ClarID/Tools/Util.pm
Criterion Covered Total %
statement 34 34 100.0
branch 5 10 50.0
condition n/a
subroutine 8 8 100.0
pod 0 2 0.0
total 47 54 87.0


line stmt bran cond sub pod time code
1             package ClarID::Tools::Util;
2              
3 36     36   257 use strict;
  36         73  
  36         1548  
4 36     36   253 use warnings;
  36         65  
  36         2313  
5 36     36   247 use Carp qw(croak);
  36         94  
  36         3046  
6 36     36   18091 use YAML::XS qw(LoadFile);
  36         131644  
  36         2944  
7 36     36   312 use JSON::XS qw(decode_json);
  36         165  
  36         2330  
8 36     36   261 use Exporter 'import';
  36         71  
  36         12737  
9             our @EXPORT_OK = qw( load_yaml_file load_json_file );
10              
11             # Load a YAML file, ensure it’s a HASHREF
12             sub load_yaml_file {
13 36     36 0 137 my ($file) = @_;
14 36         87 my $data = eval { LoadFile($file) };
  36         319  
15 36 50       60290 croak "Error loading YAML file '$file': $@" if $@;
16 36 50       769 croak "Expected a HASH in '$file'" unless ref $data eq 'HASH';
17 36         475 return $data;
18             }
19              
20             # Load a JSON file, ensure it’s a HASHREF
21             sub load_json_file {
22 36     36 0 155 my ($file) = @_;
23 36 50       2268 open my $fh, '<', $file
24             or croak "Error opening JSON file '$file': $!";
25 36         225 local $/;
26 36         61336 my $text = <$fh>;
27 36         624 close $fh;
28 36         123 my $data = eval { decode_json($text) };
  36         1374968  
29 36 50       513 croak "Error parsing JSON in '$file': $@" if $@;
30 36 50       364 croak "Expected a HASH in '$file'" unless ref $data eq 'HASH';
31 36         2467 return $data;
32             }
33              
34             1;
35