| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | ## no critic (ValuesAndExpressions::ProhibitConstantPragma) | 
| 2 |  |  |  |  |  |  | package Env::Dot; | 
| 3 | 1 |  |  | 1 |  | 6598 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 28 |  | 
| 4 | 1 |  |  | 1 |  | 5 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 47 |  | 
| 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 |  | 6 | no warnings 'redefine';    ## no critic [TestingAndDebugging::ProhibitNoWarnings] | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 69 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | sub import { | 
| 14 | 1 |  |  | 1 |  | 14 | load_vars(); | 
| 15 | 1 |  |  |  |  | 18 | return; | 
| 16 |  |  |  |  |  |  | } | 
| 17 |  |  |  |  |  |  | } | 
| 18 |  |  |  |  |  |  |  | 
| 19 | 1 |  |  | 1 |  | 535 | use English qw( -no_match_vars );    # Avoids regex performance penalty in perl 5.18 and earlier | 
|  | 1 |  |  |  |  | 1722 |  | 
|  | 1 |  |  |  |  | 6 |  | 
| 20 | 1 |  |  | 1 |  | 365 | use Carp; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 84 |  | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | # ABSTRACT: Read environment variables from .env file | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | our $VERSION = '0.006'; # VERSION: generated by DZP::OurPkgVersion | 
| 25 |  |  |  |  |  |  |  | 
| 26 | 1 |  |  | 1 |  | 369 | use Env::Dot::Functions qw( get_dotenv_vars interpret_dotenv_filepath_var ); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 74 |  | 
| 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 |  |  |  |  | 2 |  | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | sub load_vars { | 
| 38 | 1 |  |  | 1 | 1 | 2 | my $dotenv_filepath_var = q{ENVDOT_FILEPATHS}; | 
| 39 | 1 |  |  |  |  | 1 | my @dotenv_filepaths; | 
| 40 | 1 | 50 |  |  |  | 5 | if ( exists $ENV{$dotenv_filepath_var} ) { | 
| 41 | 1 |  |  |  |  | 5 | @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 |  |  |  |  | 18 | $new_env{ $var->{'name'} } = $var->{'value'}; | 
| 56 |  |  |  |  |  |  | } | 
| 57 | 1 |  |  |  |  | 9 | foreach my $var_name ( sort keys %ENV ) { | 
| 58 | 2 |  |  |  |  | 4 | $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 |  |  |  |  | 12 | %ENV = %new_env; | 
| 64 | 1 |  |  |  |  | 6 | return \%ENV; | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | 1; | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | __END__ |