in reply to Expanding environment variables from ini file

> will handle various edge cases I might not think of.

There is an extremely stable approach I used on linux to "import" the ENV from a child process.

see code samples in Re^3: How to "source" a shell file in Perl? (Trojan Dump)

You might be able to adapt it for win/batch.

No parsing whatsoever needed, just nested calls using standard mechanisms and modules.

HTH! :)

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

update

*) using Data::Dumper ,which is in core

°) by reading your INI or whatever your process is requiring

  • Comment on Re: Expanding environment variables from ini file

Replies are listed 'Best First'.
Re^2: Expanding environment variables from ini file
by Corion (Patriarch) on Aug 27, 2019 at 10:27 UTC

    Why do you need that third level?

    On unixish OSes, the env command outputs the contents of the environment (as strings, so beware of newlines) and on Windows, the set command does that. Or did you output %ENV as JSON or something?

      ...beware of newlines

      This should be a sufficient warning to avoid such a solution. There's no chance to find out whether the following env output comes from two environment variables FOO and PERL5LIB or just from setting one variable with export FOO=$(echo -e "bar\nPERL5LIB=my/malicious/stuff"):

      FOO=bar PERL5LIB=/my/malicious/stuff
      On Windows, the same misleading output can be created by typing the following (where <enter> is the "enter" key):
      SET FOO=BAR^<enter> <enter> PERL5LIB=c:/my/malicious/stuff<enter>
      Because Data::Dumper creates a clean serialization of Perl data structures which can be eval'ed directly without any parsing on the top-level.

      update

      > Or did you output %ENV as JSON or something?

      kind of "something", a Perl dump from Data::Dumper which is core (contrary to JSON)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re^2: Expanding environment variables from ini file
by Lotus1 (Vicar) on Aug 27, 2019 at 15:29 UTC

    That is a clever and interesting approach to that problem. For my situation I am able to use a function in a module I created to set the variables in the environment at the top level script. My predecessor had the batch files read the ini over and over but I'm planning to just test to see if a key is already set before re-reading the ini file.

    package custom_environment; use strict; use warnings; use Exporter; our @ISA = qw(Exporter); our @EXPORT = qw( setup_modelbuild_environment ); #################################### ### Setup environment variables from the mm config file. ### sub setup_modelbuild_environment { $ENV{test_setup_vars} = "test123"; print "sub setup_modelbuild_environment : test_setup_vars = $ENV{t +est_setup_vars}\n"; } 1;