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

hi perl monks! I have a problem and i still didn't find out if it's a perl bug or a yaml module bug. if i write a code like that:
$x->{data} = 1;
$x->{abc} = $x;
print YAML::Dump( $x, $y );


The results will be:
---
- &1    # &1 shows us that we will meet a reference to this place.
  data: 1
- abc: *1    # *1 represent the reference to the &1 ($x)


in one of my perl application i saw a yaml file that look like that:
---
Data: &1 &2
  a: 1
  b: 2
Trans:
  - *1
  - *2


which means that i have 2 different references to the same data.
the problem is that the YAML module can't read this file and i get an error ( more then one anchor ) at the line that the "Data: &1 &2" present.

is there a way to crate tow different refs to the same data in an object?

i'll be happy if anyone here can give me a direction to start investigating this bug.

Thanks, Eli Shabtay
http://eshsoftware.biz

Replies are listed 'Best First'.
Re: hash ref and yaml
by kyle (Abbot) on Feb 16, 2009 at 15:52 UTC

    As I suspected, you have one label and refer to it twice:

    use YAML; my $x = { Data => { a => 1, b => 2 } }; $x->{Trans} = [ $x->{Data}, $x->{Data} ]; print Dump( $x ); __END__ --- Data: &1 a: 1 b: 2 Trans: - *1 - *1
      But that's ok, what is not ok is that i have two
      different refers to the same label:

      ---
      Data: &1 &2
        a: 1
        b: 1
      Trans:
        - *1
        - *2

      Try to Load this YAML text above and you will get
      and error.

      Thanks,
      Eli
      http://eshsoftware.biz

        Perhaps I misunderstood your question.

        If you want to know whether a YAML document such as you have presented is valid, I suggest looking at the YAML spec to see what it says. I'm not a YAML expert, so I don't have any idea, myself.

        If you have a file like this and you want to get it loaded, I suggest fixing the file. You could also try loading it with different YAML implementations to see if any will take it.