| 1 | package ClarID::Tools::Util; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 55 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 55 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 55 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 55 | ▶ | use YAML::XS qw(LoadFile); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 55 | ▶ | use JSON::XS qw(decode_json); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 55 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 55 | ▶ | use ClarID::Tools (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 55 | our @EXPORT_OK = qw( load_yaml_file load_json_file assert_supported_codebook_version ); | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | # Load a YAML file, ensure it’s a HASHREF | ||||||||||||||||||||||||||||
| 13 | 55 | ▶ | sub load_yaml_file { | ||||||||||||||||||||||||||
|
Subroutine
load_yaml_file: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 14 | 55 | my ($file) = @_; | |||||||||||||||||||||||||||
| 15 | 55 | my $data = eval { LoadFile($file) }; | |||||||||||||||||||||||||||
| 16 | 55 | ▶ | croak "Error loading YAML file '$file': $@" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | 55 | ▶ | croak "Expected a HASH in '$file'" unless ref $data eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | 55 | assert_supported_codebook_version( $data, $file ); | |||||||||||||||||||||||||||
| 19 | 53 | return $data; | |||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 55 | ▶ | sub assert_supported_codebook_version { | ||||||||||||||||||||||||||
|
Subroutine
assert_supported_codebook_version: called CC=4 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 23 | 55 | my ( $data, $file ) = @_; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 55 | my $version = $data->{metadata}{version}; | |||||||||||||||||||||||||||
| 26 | 55 | ▶ | croak "Missing metadata.version in codebook '$file'" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $version and $version ne ""
MC/DC0%0/2
defined $version and $version ne ""
defined $version
$version ne ""
Truth table33%1/3
A and B
A= defined $versionB= $version ne ""
| |||||||||||||||||||||||||||||
| 27 | unless defined $version && $version ne ''; | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | my %supported = | ||||||||||||||||||||||||||||
| 30 | 55 | map { $_ => 1 } @ClarID::Tools::SUPPORTED_CODEBOOK_VERSIONS; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 55 | ▶ | return 1 if $supported{$version}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 2 | my $supported = join ', ', @ClarID::Tools::SUPPORTED_CODEBOOK_VERSIONS; | |||||||||||||||||||||||||||
| 35 | 2 | croak | |||||||||||||||||||||||||||
| 36 | "Unsupported codebook version '$version' in '$file'. This ClarID-Tools release supports: $supported"; | ||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # Load a JSON file, ensure it’s a HASHREF | ||||||||||||||||||||||||||||
| 40 | 48 | ▶ | sub load_json_file { | ||||||||||||||||||||||||||
|
Subroutine
load_json_file: called CC=4 SCAR=14.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 41 | 48 | my ($file) = @_; | |||||||||||||||||||||||||||
| 42 | 48 | ▶ | open my $fh, '<', $file | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | or croak "Error opening JSON file '$file': $!"; | ||||||||||||||||||||||||||||
| 44 | 48 | local $/; | |||||||||||||||||||||||||||
| 45 | 48 | my $text = <$fh>; | |||||||||||||||||||||||||||
| 46 | 48 | close $fh; | |||||||||||||||||||||||||||
| 47 | 48 | my $data = eval { decode_json($text) }; | |||||||||||||||||||||||||||
| 48 | 48 | ▶ | croak "Error parsing JSON in '$file': $@" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 48 | ▶ | croak "Expected a HASH in '$file'" unless ref $data eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 48 | return $data; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 55 | 1; | |||||||||||||||||||||||||||