blib/lib/App/envfile.pm

filter: Statementstmt 86.2% 50 / 58 Branchbran 77.2% 17 / 22 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 90.0% 9 / 10 Podpod 100% 3 / 3 totaltotal 85.4% 82 / 96 CC 14 SCAR 15.4
CC
14
Cov
84%
parse_envfile
7.0
_try_any_config_file
6.1
_slurp
6.0
CRAP 4.7
↑ Summary
1 package App::envfile;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 our $VERSION = '0.07';
9  
10 5 our $EXTENTIONS_MAP = {
11 pl => 'Perl',
12 perl => 'Perl',
13 js => 'JSON',
14 json => 'JSON',
15 yml => 'YAML',
16 yaml => 'YAML',
17 };
18  
19 13 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
20 13 my $class = shift;
21 13 bless {}, $class;
22 }
23  
24 2 ▶sub run_with_env {
Subroutine run_with_env: called CC=2 SCAR=6.9
Pod documented
25 2 my ($self, $env, $commands) = @_;
26 2 local %ENV = %ENV;
27 2 for my $key (keys %$env) {
28 3 $ENV{$key} = $env->{$key};
29 }
30 2 exec(@$commands);
31 }
32  
33 9 ▶sub parse_envfile {
Subroutine parse_envfile: called CC=7 SCAR=19.5
Pod documented
34 9 my ($self, $file) = @_;
35 9 ▶ Carp::croak "Usage: $self->parse_envfile(\$file)" unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 9 9
36  
37 9 my $env = {};
38 9 ▶ return $env if $env = $self->_try_any_config_file($file);
Branch: 1 branch
BranchTrueFalseTotal
if $env = $self->_try_any_config_file($file) 0 9 9
39  
40 9 ▶ open my $fh, '<', $file or Carp::croak "$file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or Carp::croak("${file}: $!") 8 1 9
41 8 ▶ while (defined (my $line = readline $fh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 12 8 20
42 12 chomp $line;
43 12 ▶ next if index($line, '#') == 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($line, "#") == 0 1 11 12
44 11 ▶ next if $line =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 2 9 11
45 9 my ($key, $value) = $self->_parse_line($line);
46 9 $env->{$key} = $value;
47 }
48 8 close $fh;
49  
50 8 return $env;
51 }
52  
53 16 ▶sub _try_any_config_file {
Subroutine _try_any_config_file: called CC=6 SCAR=18.1
54 16 my ($self, $file) = @_;
55  
56 16 my ($ext) = $file =~ /\.(\w+)$/;
57 16 ▶ if (my $type = $EXTENTIONS_MAP->{lc($ext || '')}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = $$App::envfile::EXTENTIONS_MAP{lc($ext || "")}) 5 11 16
Condition100%2/2
$ext || ''
l!l
79
MC/DC100%1/1
$ext || ''
$ext
Truth table100%2/2
A
A= $ext
Aresult
11
00
58 5 my $env;
59 5 ▶ if ($type eq 'Perl') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'Perl') { } 5 0 5
60 5 $env = do "$file";
61 5 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 4 5
62 }
63 else {
64 0 require Data::Encoder;
65 0 $env = Data::Encoder->load($type)->decode($self->_slurp($file));
66 }
67 4 ▶ die "$file: Should be return HASHREF\n" unless ref $env eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $env eq "HASH" 2 2 4
68 2 return $env;
69 }
70  
71 11 return;
72 }
73  
74 0 ▶sub _slurp {
Subroutine _slurp: not called CC=2 SCAR=17.9
75 0 my ($self, $file) = @_;
76 0 my $data = do {
77 0 local $\;
78 0 ▶ open my $fh, '<', $file or die "$file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "${file}: $!\n" 0 0 0
79 0 <$fh>;
80 };
81 0 return $data;
82 }
83  
84 9 ▶sub _parse_line {
Subroutine _parse_line: called CC=1 SCAR=0.0
85 9 my ($self, $line) = @_;
86 9 my ($key, $value) = map { my $str = $_; $str =~ s/^\s+|\s+$//g; $str } split '=', $line, 2;
87 9 return $key, $value;
88 }
89  
90 5 1;
91 __END__
↑ Summary