http://www.perlmonks.org?node_id=945538

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

Hello everybody,

I have a problem using YAML, in particular I want to save the values with also the hash variable name.

Example in my code I have the following code:
use YAML qw'DumpFile LoadFile'; . . . DumpFile("test.yaml", \%test);
In the generated test.yaml I have always the following information:
--- key1: value1 key2: value2
But I would like to have the following datas:
test: key1: value1 key2: value2

In particular I would substitute the --- with the name of hash variable.

How could I generate this output in the file? Is it possible? Thanks in advance Pazzeo

Replies are listed 'Best First'.
Re: Problem using YAML
by choroba (Archbishop) on Dec 29, 2011 at 18:41 UTC
    Of course it is possible. But then it is not YAML anymore.
    use YAML; %test = (k1 => "v1", k2 => "v2"); $dump = Dump \%test; $dump =~ s/^---/test:/; print $dump;
      Thanks..using YAML is it not possible?

      Should I change method?

      I have a file with datas, that I import in the perl code (hash variable) and then I save in the end the variable hash into the same file. So I thought that YAML was the best solution to perfom it.

      Thanks

        Because YAML has a particular format, delineated with the "---", you will have to add a top level key that contains the contents you desire. When You decode, you will have to deference the hash key that this corresponds to. For example:
        --- This: top level mapping is: - a - YAML - document
        will decode to a hash that looks like:
        my $yaml_hash_ref = { This => 'top level mapping', is => ['a', 'yaml', 'document'] };
        Therefore, if you want "%This" to be your hash variable, you need to do this:
        my %This = %{$yaml_hash_ref->{This}};
        If you want the whole structure, you just use whatever YAML::decode gives you, i.e., YAML's encode doesn't take into account the name of your variable containing the data structure to convert to YAML. Similarly, decode returns a data structure reference (scalar, array, or hash) depending on how complex (e.g., hash references contain all non-trivial data structures implied by the YAML).
        I'm using YAML in my example, too. It depends on your definition of "using".

        Imagine you want to use English, but you use "mwa" instead of "the". Are you still using English?

Re: Problem using YAML
by Anonymous Monk on Dec 29, 2011 at 18:48 UTC

    In particular I would substitute the --- with the name of hash variable.

    No you wouldn't

    $ perl -MYAML -e " print Dump { test => { 1 .. 4 } } " --- test: 1: 2 3: 4 $ perl -MYAML -e " print Dump yaml => { test => { 1 .. 4 } } " --- yaml --- test: 1: 2 3: 4 $ perl -MDDS -mYAML -e " Dump( YAML::Load( YAML::Dump( { test => { 1 . +. 4 } } ) ) )" $HASH1 = { test => { 1 => 2, 3 => 4 } }; $ perl -MDDS -mYAML -e " Dump( YAML::Load( YAML::Dump( yaml => { test +=> { 1 .. 4 } } ) ) )" $VAR1 = 'yaml'; $HASH1 = { test => { 1 => 2, 3 => 4 } };
Re: Problem using YAML
by anneli (Pilgrim) on Dec 31, 2011 at 02:55 UTC

    You can't get rid of ---, but what about like this?

    DumpFile("test.yaml", { test => \%test })

    DumpFile can't see the name of the variable you used, so you have to put that in the datastructure itself.