ENV-Util

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

Revision history for ENV-Util

0.04    2023-12-19
    OTHER:
        - typo fix in documentation (Ron Savage)

0.03    2023-08-11
    NEW FEATURES:
        - tcsh/csh support for 'env' and 'setenv' keywords.
        - variable interpolation support.
    OTHER:
        - document .env file format

0.02    2023-08-10
    OTHER:
        - add missing files.

0.01    2023-08-10
    - Initial release!

lib/ENV/Util.pm  view on Meta::CPAN


    my @lines;
    { local $!; @lines = <$fh> }
    my %env;
    # POSIX convention for env variable names:
    my $varname_re = qr/[a-zA-Z_][a-zA-Z0-9_]+/;
    foreach my $line (@lines) {
        # code heavily inspired by Dotenv.pm (BooK++)
        if (my ($k, $v) = $line =~ m{
            \A\s*
            # 'export' (bash), 'set'/'setenv' ([t]csh) are optional keywords:
            (?: (?:export|set|setenv) \s+ )?
            ( $varname_re )
            (?: \s* (?:=|\s+) \s* )       # separator is '=' or spaces
            (
              '[^']*(?:\\'|[^']*)*'  # single quoted value
             |"[^"]*(?:\\"|[^"]*)*"  # or double quoted value
             | [^\#\r\n]+            # or unquoted value
            )?
            \s* (?: \# .* )?            # inline comment
            \z}sx
        ) {

lib/ENV/Util.pm  view on Meta::CPAN

    FOO  =  some value   # spaces are trimmed except inside the string.
    FOO='some value'     # so both lines here are the same.

    NEWLINE=\n           # unquoted values are treated WITH interpolation.
    NEWLINE="\n"         # so this line and the one above are the same.
    LITERAL='\n'         # but this is a literal '\' and a literal 'n'.

    BAR=baz          # bash/zsh format.
    export BAR=baz   # bash format.
    set BAR=baz      # tcsh/csh format.
    setenv BAR baz   # tcsh/csh format (note this one lacks '=').

    # empty values are set to the empty string ''
    NONE =
    ALSONONE

    # you can prepend '$' to variable names to interpolate them
    # in unquoted or double quoted values. You can point to variables
    # declared before in the file or available in your %ENV
    # *AS LONG AS* you don't try to replace variables from %ENV,
    # which is not allowed by design.

t/data/test.env  view on Meta::CPAN

# this .env file is used to test ENV::Util.

FOO=BAR

BAR=bar with comments   # inline comments!

K = 42 # this is NOT be replaced because %ENV already has 'K'

export DOTEXPORT = env  # ignore 'export'
set DOTSET = env        # ignore 'set'
setenv DOTSETENV env

SOME_KEY= /some/value

QUOTED_KEY ='this is quoted\n'
DOUBLY="this is doubly quoted\n"

# variable interpolation
PATH=$FOO$EXTERNAL
export PATH=$PATH:foo:$PATH
set PATH=foo:$PATH
setenv PATH $PATH:foo

LITERAL = '$FOO'
NONE =
NONEXISTANT = $IDONTEXIST
NOTEVENEQUAL



( run in 1.128 second using v1.01-cache-2.11-cpan-3989ada0592 )