quietgecko has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

Is there a way in which environment variables are recognized by YAML files

use YAML; my ($hashref) = Load(<<'...'); --- Home: "$ENV{HOME}" ... print Dump ($hashref);

The above snippet results in:

HOME: '$ENV{HOME}'
Hence the environment variable is not interpolated.
Is there anyway this can be done or should we post process the hash to resolve the environment variables
Thanks

UPDATE: Thanks Everyone for your replies. Greatly appreciate it.
As MidLifeXis pointed out, I was only providing a self contained example.
In reality I have the YAML as a separate file that will have a lot of entries.
But as afoken and RonW pointed out, It looks like interpolation is not supported and we may need to do data manipulation
Here is a snippet that mimics my actual case

use YAML; my ($hashref) = YAML::LoadFile("test.yaml"); print Dump ($hashref); print $h;

The content of the test.yaml:

Home: "$ENV{HOME}"

Replies are listed 'Best First'.
Re: Environment Variables in YAML
by NetWallah (Canon) on Nov 17, 2015 at 05:58 UTC
    Change single quotes to Double in this line, and it works:
    my ($hashref) = Load(<<"...");
    From "<<EOF" (here-doc) in perlop:

    Double quotes indicate that the text will be interpolated using exactly the same rules as normal double quoted strings.

            “The sources of quotes found on the internet are not always reliable.” — Abraham Lincoln.3; cf.

      ...which works in the case of the sample provided in the original post; in the case of "YAML files" (emphasis added), as was also stated in the original post, it will not. The OP was providing a self contained example (kudos to the OP).

      --MidLifeXis

        Ah - I missed that subtle reference to YAML "Files". Thanks for pointing that out.

        But if you are creating a YAML file, why would you embed environment variables like that ? Sounds like a case for using a template style tool.

        I'm leaving it to the OP to post a snippet closer to his actual circumstances.

                “The sources of quotes found on the internet are not always reliable.” — Abraham Lincoln.3; cf.

Re: Environment Variables in YAML
by afoken (Chancellor) on Nov 17, 2015 at 05:10 UTC

    No. But you could do that after reading the YAML file, using s///. Something like this:

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $hashref={ ABC => 123, PATH => '$ENV{PATH}' }; print Dumper($hashref); s/\$ENV{(.+?)}/$ENV{$1}/g for values %$hashref; print Dumper($hashref);

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Environment Variables in YAML
by RonW (Parson) on Nov 17, 2015 at 18:02 UTC

    afoken is correct that you can't. The reason is that the YAML specification doesn't provide for interpolation. It's just a format for serializing data for storage or transport. What you put in is what you get out.

    Interpolation or other manipulation of the data is the responsibility of the application.

Re: Environment Variables in YAML
by MidLifeXis (Monsignor) on Nov 18, 2015 at 14:39 UTC

    Some of the modules for configuration in CPAN allow for variable interpolation. Not certain if any of the YAML configuration modules are post processed to handle tokens within. You will need to dig a bit (or use the LazyWeb :-D ) to find that answer.

    --MidLifeXis