NAME
Env::Dot - Read environment variables from .env file
VERSION
version 0.008
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 the 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 :. Dot::Env will load all the files in the order you specify them.
- 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 Dot::Env package in directory script.
eval "$(envdot)"
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.
- 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 are automatically 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 is another package which implements functionality to use .env files in Perl.
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.