Env::Dot - Read environment variables from .env file
version 0.007
use Env::Dot; print $ENV{'VAR_DEFINED_IN_DOTENV_FILE'};
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
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.
If your .env file is located in another path, not the current working directory, you can use the environment variable DOTENV_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.
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.
The executable is distributed together with Dot::Env package in directory script.
eval "$(envdot)"
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.
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
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}"
Package Env::Dot is currently being developed so changes in the API are possible, though not likely.
No external dependencies outside Perl's standard distribution.
No functions are automatically exported to the calling namespace.
Load variables from .env file or files in environment variable ENVDOT_FILEPATHS.
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.
Mikko Koivunalho <mikkoi@cpan.org>
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.
To install Env::Dot, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Env::Dot
CPAN shell
perl -MCPAN -e shell install Env::Dot
For more information on module installation, please visit the detailed CPAN module installation guide.