Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Problem using YAML

by choroba (Archbishop)
on Dec 29, 2011 at 18:41 UTC ( #945540=note: print w/replies, xml ) Need Help??


in reply to Problem using YAML

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;

Replies are listed 'Best First'.
Re^2: Problem using YAML
by Pazzeo (Initiate) on Dec 29, 2011 at 18:53 UTC
    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?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://945540]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2022-08-14 21:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?