Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Problem using YAML

by Pazzeo (Initiate)
on Dec 29, 2011 at 18:11 UTC ( #945538=perlquestion: print w/replies, xml ) Need Help??

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.


        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.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://945538]
Approved by Corion
Front-paged by delirium
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2022-06-30 00:46 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (97 votes). Check out past polls.