| 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
| |||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 9 | my $env = {}; | |||||||||||||||||||
| 38 | 9 | ▶ | return $env if $env = $self->_try_any_config_file($file); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 9 | ▶ | open my $fh, '<', $file or Carp::croak "$file: $!"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | 8 | ▶ | while (defined (my $line = readline $fh)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 42 | 12 | chomp $line; | |||||||||||||||||||
| 43 | 12 | ▶ | next if index($line, '#') == 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 44 | 11 | ▶ | next if $line =~ /^\s*$/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
Condition100%2/2
$ext || ''
MC/DC100%1/1
$ext || ''
$ext
Truth table100%2/2
A
A= $ext
| |||||||||||||||||||||
| 58 | 5 | my $env; | |||||||||||||||||||
| 59 | 5 | ▶ | if ($type eq 'Perl') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 60 | 5 | $env = do "$file"; | |||||||||||||||||||
| 61 | 5 | ▶ | die $@ if $@; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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__ | ||||||||||||||||||||