NAME

Env::Dot - Read environment variables from .env file

VERSION

version 0.014

SYNOPSIS

use Env::Dot;

print $ENV{'VAR_DEFINED_IN_DOTENV_FILE'};

DESCRIPTION

More flexibility in how you manage and use your .env file.

Attn. Existing environment variables always take precedence to dotenv variables! A dotenv variable (variable from a file) does not overwrite an existing environment variable. This is by design because a dotenv file is to augment the environment, not to replace it.

This means that you can override a variable in `.env` file by creating its counterpart in the environment. For instance:

unset VAR
echo "VAR='Good value'" >> .env
perl -e 'use Env::Dot; print "VAR:$ENV{VAR}\n";'
# VAR:Good value
VAR='Better value'; export VAR
perl -e 'use Env::Dot; print "VAR:$ENV{VAR}\n";'
# VAR:Better value

Features

If no .env file is present, then do nothing

By default, Env::Dot will do nothing if there is no .env file. You can also configure Env::Dot to emit an alarm or break execution, if you want.

Specify other dotenv files with path

If your .env file is located in another path, not the current working directory, you can use the environment variable ENVDOT_FILEPATHS to tell where your dotenv file is located. You can specify several file paths; just separate them by :. Env::Dot will load the files in the reverse order, starting from the last. This is the same ordering as used in PATH variable: the first overrules the following ones, that is, when reading from the last path to the first path, if same variable is present in more than one file, the later one replaces the one already read.

For example, if you have the following directory structure:

project-root
| .env
+ - sub-project
  | .env

and you specify ENVDOT_FILEPATHS=project-root/sub-project/.env:project-root/.env, then the variables in file project-root/.env will get replaced by the more specific variables in project-root/sub-project/.env

N.B. The ordering has changed in version 0.0.9.

Support different types of .env files

Unix Shell source command compatible dotenv files use double or single quotation marks (" or ') to define a variable which has spaces. But, for instance, Docker compatible .env files do not use quotation marks. The variable's value begins with = sign and ends with linefeed.

You can specify in the dotenv file itself - by using meta commands - which type of file it is.

Use executable envdot to bring the variables into your shell

The executable is distributed together with Env::Dot package. It is in the directory script.

eval "$(envdot)"

N.B. If your .env file(s) contain variables which need interpolating, for example, to combine their value from other variables or execute a command to produce their value, you have to use the envdot program. Env::Dot does not do any interpolating. It cannot because that would involve running the variable in the shell context.

DotEnv File Meta Commands

The var: commands affect only the subsequent variable definition. If there is another envdot command, the second overwrites the first and default values are applied again.

read:from_parent

By setting this option to true, Env::Dot or envdot command will search for .env files in the file system tree upwards. It will load the first .env file it finds from the current directory upwards to root.

Using read:from_parent will only find and read one .env file in a parent directory. If you want to chain the .env files, they all must set read:from_parent - except the top one.

This functionality can be useful in situations where you have parallel projects which share common environment variables in one .env file in a parent directory.

If there is no parent .env file, Env::Dot will break execution and give an error.

By default this setting is off.

read:allow_missing_parent

When using option read:from_parent, if the parent .env file does not exist, by default Env::Dot will emit an error and break execution. In some situations, it might be normal that a parent .env file could be missing. Turn on option read:allow_missing_parent if you do not want an error in that case.

By default this setting is off.

file:type

Changes how Env::Dot reads lines below from this commands. Default is:

# envdot (file:type=shell)
VAR="value"

Other possible value of file:type is:

# envdot (file:type=plain)
VAR=My var value
var:allow_interpolate

By default, when writing variable definitions for the shell, every variable is treated as static and surrounded with single quotation marks ' in Unix shell which means shell will read the variable content as is. By setting this to 1 or true, you allow shell to interpolate. This meta command is only useful when running envdot command to create variable definitions for eval command to read.

# envdot (var:allow_interpolate)
DYNAMIC_VAR="$(pwd)/${ANOTHER_VAR}"

STATUS

Package Env::Dot is currently being developed so changes in the API are possible, though not likely.

DEPENDENCIES

No external dependencies outside Perl's standard distribution.

FUNCTIONS

No functions exported to the calling namespace.

load_vars

Load variables from .env file or files in environment variable ENVDOT_FILEPATHS.

SEE ALSO

Env::Assert will verify that you certainly have those environmental variables you need. It also has an executable which can perform the check in the beginning of a docker container run.

Dotenv and ENV::Util are packages which also implement functionality to use .env files in Perl.

Config::ENV and Config::Layered::Source::ENV provide other means to configure application with the help of environment variables.

AUTHOR

Mikko Koivunalho <mikkoi@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2023 by Mikko Koivunalho.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.