File Coverage

blib/lib/Env/Dot.pm
Criterion Covered Total %
statement 35 37 94.5
branch 1 4 25.0
condition n/a
subroutine 9 9 100.0
pod 1 1 100.0
total 46 51 90.2


line stmt bran cond sub pod time code
1             ## no critic (ValuesAndExpressions::ProhibitConstantPragma)
2             package Env::Dot;
3 1     1   6939 use strict;
  1         2  
  1         30  
4 1     1   4 use warnings;
  1         2  
  1         57  
5              
6             # We define our own import routine because
7             # this is the point (when `use Env::Dot` is called)
8             # when we do our magic.
9              
10             {
11 1     1   5 no warnings 'redefine'; ## no critic [TestingAndDebugging::ProhibitNoWarnings]
  1         3  
  1         73  
12              
13             sub import {
14 1     1   8 load_vars();
15 1         18 return;
16             }
17             }
18              
19 1     1   498 use English qw( -no_match_vars ); # Avoids regex performance penalty in perl 5.18 and earlier
  1         1733  
  1         6  
20 1     1   356 use Carp;
  1         3  
  1         75  
21              
22             # ABSTRACT: Read environment variables from .env file
23              
24             our $VERSION = '0.005'; # VERSION: generated by DZP::OurPkgVersion
25              
26 1     1   359 use Env::Dot::Functions qw( get_dotenv_vars interpret_dotenv_filepath_var );
  1         2  
  1         76  
27              
28             use constant {
29 1         255 OPTION_FILE_TYPE => q{file:type},
30             OPTION_FILE_TYPE_PLAIN => q{plain},
31             OPTION_FILE_TYPE_SHELL => q{shell},
32             DEFAULT_OPTION_FILE_TYPE => q{shell},
33             DEFAULT_DOTENV_FILEPATH => q{.env},
34             INDENT => q{ },
35 1     1   7 };
  1         3  
36              
37             sub load_vars {
38 1     1 1 2 my $dotenv_filepath_var = q{ENVDOT_FILEPATHS};
39 1         2 my @dotenv_filepaths;
40 1 50       3 if ( exists $ENV{$dotenv_filepath_var} ) {
41 1         4 @dotenv_filepaths = interpret_dotenv_filepath_var( $ENV{$dotenv_filepath_var} );
42             }
43             else {
44 0 0       0 if ( -f DEFAULT_DOTENV_FILEPATH ) {
45 0         0 @dotenv_filepaths = (DEFAULT_DOTENV_FILEPATH); # The CLI parameter
46             }
47             }
48              
49 1         3 my @vars = get_dotenv_vars(@dotenv_filepaths);
50 1         2 my %new_env;
51              
52             # Populate new env with the dotenv variables.
53 1         2 foreach my $var (@vars) {
54             ### no critic [Variables::RequireLocalizedPunctuationVars]
55 4         9 $new_env{ $var->{'name'} } = $var->{'value'};
56             }
57 1         6 foreach my $var_name ( sort keys %ENV ) {
58 2         5 $new_env{$var_name} = $ENV{$var_name};
59             }
60              
61             # We need to replace the current %ENV, not change individual values.
62             ## no critic [Variables::RequireLocalizedPunctuationVars]
63 1         11 %ENV = %new_env;
64 1         7 return \%ENV;
65             }
66              
67             1;
68              
69             __END__