## RE: [Yaml-core] YAML 1.0 (Working Draft 31 Oct 2002)

 RE: [Yaml-core] YAML 1.0 (Working Draft 31 Oct 2002) From: Rich Morin - 2002-12-25 18:59:49 ```At 1:24 PM +0200 12/25/02, Oren Ben-Kiki wrote: >Here's a simple cycle: > >--- &HASH >key: *HASH # Value of key is the hash itself >... Fine, but let's try something a bit more complex. The code below creates two nodes which point to each other. My own print statements indicate that the structure is circular. YAML::Dump is also happy to print out the structure, but to be honest, I have no idea what the YAML output means. An annotated version of this might go a long way toward making the process comprehensible... -r % cat tst #!/usr/bin/env perl use strict; { my (%foo, \$r1, \$r2); \$r1 = \\$foo{one}; \$r2 = \\$foo{two}; \$foo{one} = [ 1, \$r2 ]; \$foo{two} = [ 2, \$r1 ]; print "\\$foo{one}[0]=\$foo{one}[0]\n"; print "\\$foo{two}[0]=\$foo{two}[0]\n"; print "\\${\\$foo{one}[1]}[0]=\${\$foo{one}[1]}->[0]\n"; print "\\${\\$foo{two}[1]}[0]=\${\$foo{two}[1]}->[0]\n"; print YAML::Dump(%foo); } % tst \$foo{one}[0]=1 \$foo{two}[0]=2 \${\$foo{one}[1]}[0]=2 \${\$foo{two}[1]}[0]=1 --- #YAML:1.0 one --- #YAML:1.0 &1 - 1 - !perl/ref: =: - 2 - !perl/ref: =: *1 --- #YAML:1.0 two --- #YAML:1.0 &1 - 2 - !perl/ref: =: - 1 - !perl/ref: =: *1 -- email: rdm@...; phone: +1 650-873-7841 http://www.cfcl.com/rdm - my home page, resume, etc. http://www.cfcl.com/Meta - The FreeBSD Browser, Meta Project, etc. http://www.ptf.com/dossier - Prime Time Freeware's DOSSIER series http://www.ptf.com/tdc - Prime Time Freeware's Darwin Collection ```

 [Yaml-core] YAML 1.0 (Working Draft 31 Oct 2002) From: Rich Morin - 2002-12-24 20:33:23 ```In scanning this document, I noticed a few problems. I can't find any pointer to a better address for bug reports, so here they are ... printing under IE 5.2 for Mac For some reason, several of the examples don't print well under IE. Specifically, the enclosing rectangle gets narrowed to the point that it overlaps the last printing character in the longest line. D6, E1, and E2 have this problem. typo In the example for section 3.2.2, the word 'three' is misspelled 'tree'. rendering cycles It may be just me, but I can't see how YAML can render cycles, given the restrictions on aliases (section 3.4). I would like to see an example that shows how to do this. -r -- email: rdm@...; phone: +1 650-873-7841 http://www.cfcl.com/rdm - my home page, resume, etc. http://www.cfcl.com/Meta - The FreeBSD Browser, Meta Project, etc. http://www.ptf.com/dossier - Prime Time Freeware's DOSSIER series http://www.ptf.com/tdc - Prime Time Freeware's Darwin Collection ```
 RE: [Yaml-core] YAML 1.0 (Working Draft 31 Oct 2002) From: Oren Ben-Kiki - 2002-12-25 11:25:12 ```Rich Morin wrote: > printing under IE 5.2 for Mac It is next to impossible to ensure that an HTML file will print properly under all browsers/operating systems. What we'll eventually do is generate a PDF version of the files for printing (this has its own warts, such as generating both A4 and a Letter versions). I'm starting to understand why the IETF insists on all RFCs being simple ASCII text files... > In the example for section 3.2.2, the word 'three' is > misspelled 'tree'. Thanks. Will be corrected. > It may be just me, but I can't see how YAML can render > cycles, given > the restrictions on aliases (section 3.4). I would like to see an > example that shows how to do this. Here's a simple cycle: --- &HASH key: *HASH # Value of key is the hash itself ... Have fun, Oren Ben-Kiki ```
 RE: [Yaml-core] YAML 1.0 (Working Draft 31 Oct 2002) From: Rich Morin - 2002-12-25 18:59:49 ```At 1:24 PM +0200 12/25/02, Oren Ben-Kiki wrote: >Here's a simple cycle: > >--- &HASH >key: *HASH # Value of key is the hash itself >... Fine, but let's try something a bit more complex. The code below creates two nodes which point to each other. My own print statements indicate that the structure is circular. YAML::Dump is also happy to print out the structure, but to be honest, I have no idea what the YAML output means. An annotated version of this might go a long way toward making the process comprehensible... -r % cat tst #!/usr/bin/env perl use strict; { my (%foo, \$r1, \$r2); \$r1 = \\$foo{one}; \$r2 = \\$foo{two}; \$foo{one} = [ 1, \$r2 ]; \$foo{two} = [ 2, \$r1 ]; print "\\$foo{one}[0]=\$foo{one}[0]\n"; print "\\$foo{two}[0]=\$foo{two}[0]\n"; print "\\${\\$foo{one}[1]}[0]=\${\$foo{one}[1]}->[0]\n"; print "\\${\\$foo{two}[1]}[0]=\${\$foo{two}[1]}->[0]\n"; print YAML::Dump(%foo); } % tst \$foo{one}[0]=1 \$foo{two}[0]=2 \${\$foo{one}[1]}[0]=2 \${\$foo{two}[1]}[0]=1 --- #YAML:1.0 one --- #YAML:1.0 &1 - 1 - !perl/ref: =: - 2 - !perl/ref: =: *1 --- #YAML:1.0 two --- #YAML:1.0 &1 - 2 - !perl/ref: =: - 1 - !perl/ref: =: *1 -- email: rdm@...; phone: +1 650-873-7841 http://www.cfcl.com/rdm - my home page, resume, etc. http://www.cfcl.com/Meta - The FreeBSD Browser, Meta Project, etc. http://www.ptf.com/dossier - Prime Time Freeware's DOSSIER series http://www.ptf.com/tdc - Prime Time Freeware's Darwin Collection ```
 RE: [Yaml-core] YAML 1.0 (Working Draft 31 Oct 2002) From: Oren Ben-Kiki - 2002-12-25 20:23:48 ```Rich Morin wrote: > Fine, but let's try something a bit more complex. The code > below creates two nodes which point to each other. My own > print statements indicate that the structure is circular. > YAML::Dump is also happy to print out the structure, but to > be honest, I have no idea what the YAML output means. An > annotated version of this might go a long way toward making > the process comprehensible... Sure thing. First, realize that while YAML supports aliases (roughly the same as Java references, except they aren't really called references :-), Perl (and C/C++) also have the related, more powerful notion of pointers (called references in Perl and are different from C++ references :-). In fact, AFAIK Perl has a stronger pointer notion than C/C++ - the last time I tried to fully grok it I got a headache :-) The basic construct used to cover Perl pointers is "!perl/ref { = : }" meaning "A Perl reference to ". With that in mind... > % cat tst > #!/usr/bin/env perl > > use strict; > > { > my (%foo, \$r1, \$r2); > > \$r1 = \\$foo{one}; > \$r2 = \\$foo{two}; > > \$foo{one} = [ 1, \$r2 ]; > \$foo{two} = [ 2, \$r1 ]; > I'd be much more comfortable if the code was written as: my \$one = [ 1 ]; my \$two = [ 2, \$one ]; \$one->[1] = \$two; \$foo{one} = \$one; \$foo{two} = \$two; I think there's a subtle difference between the two (Brian - could you help me out here?). At any rate... > print "\\$foo{one}[0]=\$foo{one}[0]\n"; > print "\\$foo{two}[0]=\$foo{two}[0]\n"; > > print "\\${\\$foo{one}[1]}[0]=\${\$foo{one}[1]}->[0]\n"; > print "\\${\\$foo{two}[1]}[0]=\${\$foo{two}[1]}->[0]\n"; > > print YAML::Dump(%foo); Didn't you mean YAML::Dump(\%foo) - dumping the whole hash table as one document? As you wrote it, I think Perl expanded the argument list to be YAML::Dump('one', \$foo{one}, 'two', \$foo{two}) which gets dumped as four separate documents. That's not a YAML issue, it is a Perl issue... But continuing with your result: > } > % tst > \$foo{one}[0]=1 > \$foo{two}[0]=2 > \${\$foo{one}[1]}[0]=2 > \${\$foo{two}[1]}[0]=1 # A document containing a string with the value "one". > --- #YAML:1.0 one # A document containing a sequence named '1' > --- #YAML:1.0 &1 # Whose first entry is the integer '1' > - 1 # And whose second entry is a pointer to > - !perl/ref: # A sequence > =: # Whose first entry is the integer 2 > - 2 # And whose second entry is a pointer to > - !perl/ref: # The sequence named '1' (the top level sequence) # (that is, here's your cycle) > =: *1 # A document containing a string with the value "two". > --- #YAML:1.0 two # A document containing a sequence named '1' # (unrelated to the previous sequence named '1', # because they belong to different documents) > --- #YAML:1.0 &1 # Whose first entry is the integer '2' > - 2 # And whose second entry is a pointer to > - !perl/ref: # A sequence > =: # Whose first entry is the integer '1' > - 1 # And whose second entry is a pointer to > - !perl/ref: # The sequence named '1' (the top level sequence) # (the one in this document of course) # (here's another cycle) > =: *1 Note how some of the data is duplicated (in the second and fourth documents). That's because documents are disjoint - there can't be any alias on one document referring to a node in another document. If you had written YAML::Dump(\\$foo) I think you would have got: --- #YAML:1.0 one: &1 - 1 - !perl/ref: =: &2 - 2 - !perl/ref: =: *1 two: *2 ... Or maybe it would be: --- #YAML:1.0 one: &1 - 1 - &2 - 2 - *1 two: *2 ... I really need Brian to tell which one best describes the Perl data structure you created. In fact, looking at these two YAML documents above makes it clearer what the issues are :-) Have fun, Oren Ben-Kiki ```
 Re: [Yaml-core] YAML 1.0 (Working Draft 31 Oct 2002) From: Brian Ingerson - 2002-12-25 22:11:23 ```On 25/12/02 22:23 +0200, Oren Ben-Kiki wrote: > Rich Morin wrote: > > If you had written YAML::Dump(\\$foo) I think you would have got: Right, Oren. Passing a hash to a Perl function listifies it. You always want to be dealing with hash references in Perl. This makes sense because, for instance, in Perl you can't have a hash pointing to another hash or array. It can only point to a reference of one. That's why in YAML land, whenever I speak of a "hash", I really mean a ref to a hash. In other languages like Python you don't have this rather silly distinction. In Perl's case it stems out of the legacy of Perl 4.0 and beyond. Compare YAML::Dump(%INC): --- #YAML:1.0 warnings/register.pm --- #YAML:1.0 |- /usr/lang/perl/5.8.0/lib/5.8.0/warnings/register.pm --- #YAML:1.0 bytes.pm --- #YAML:1.0 '/usr/lang/perl/5.8.0/lib/5.8.0/bytes.pm' --- #YAML:1.0 XSLoader.pm --- #YAML:1.0 |- /usr/lang/perl/5.8.0/lib/5.8.0/i686-linux/XSLoader.pm --- #YAML:1.0 Carp.pm to YAML::Dump(\%INC): --- #YAML:1.0 XSLoader.pm: |- /usr/lang/perl/5.8.0/lib/5.8.0/i686-linux/XSLoader.pm bytes.pm: '/usr/lang/perl/5.8.0/lib/5.8.0/bytes.pm' warnings/register.pm: |- /usr/lang/perl/5.8.0/lib/5.8.0/warnings/register.pm > > --- #YAML:1.0 > one: &1 > - 1 > - !perl/ref: > =: &2 > - 2 > - !perl/ref: > =: *1 > two: *2 > ... > > Or maybe it would be: > > --- #YAML:1.0 > one: &1 > - 1 > - &2 > - 2 > - *1 > two: *2 > ... > > I really need Brian to tell which one best describes the Perl data > structure you created. In fact, looking at these two YAML documents > above makes it clearer what the issues are :-) Or you could just run the code :) --- #YAML:1.0 one: &1 - 1 - !perl/ref: =: &2 - 2 - !perl/ref: =: *1 two: *2 Notso pretty, but pretty nutso. I don't think you can create a rooted graph that can't be expressed in YAML. I could be wrong, but I leave the proof to you. YAML.pm recurses down the graph, dumping everything the first time it sees it, and emitting an alias for all subsequent occurences. I prewalk the graph to see where the anchors need to go. This way the aliases always point upwards, as mandated by YAML. Pretty simple actually. BTW, I think YAML is at least as good as Data::Dumper in this regard. A couple things to keep in mind. Aliases don't span documents. In Perl, a list of things is Dumped as a separate document for each item. I don't consider this a limitation, because you can always Dump the list as an array (reference) to preserve the aliases. Also YAML.pm does not currently preserve the identity of scalars used more than once. This does not occur naturally in Perl very often because the Perl internals do a copy on store when inserting scalars into collections. Even if there did happen to be the same scalar twice in a stucture, it is 9 times out of ten more useful to redump the scalar than do an alias of it. I may add a "Purity" pragma some day, but for now it's definitely YAGNI! --- Cheers: Brian ```
 RE: [Yaml-core] YAML 1.0 (Working Draft 31 Oct 2002) From: Oren Ben-Kiki - 2002-12-26 20:10:45 ```> Brian Ingerson wrote: > > I really need Brian to tell which one best describes the Perl data > > structure you created. In fact, looking at these two YAML documents > > above makes it clearer what the issues are :-) > > Or you could just run the code :) Yeah, but I just re-installed my computer and for some reason CPAN wasn't responding that day so I couldn't install the YAML.pm module. Next time :-) Have fun, Oren Ben-Kiki ```