YAML with perl (2)

YAML.pm がなんか怪しげだったので YAML::XS でも試してみる。YAML.pm で結果が怪しげだったところ中心に。テストプラットホームも変えて YAML::XS v0.27, perl v5.8.8 on FreeBSD 7.0-RELEASE で。
参考

結論から行くと、上の参考ページと同じ。

  • YAML.pm よりは YAML::XS の方がまともに動く。
  • tag関係が効かないのは変わらず。

といったところか。こうしてみると YAML.pm って激しく buggy なんだよね。


Example 2.2. Mapping Scalars to Scalars(player statistics)

hr:  65    # Home runs
avg: 0.278 # Batting average
rbi: 147   # Runs Batted In
##### yaml #####
---
avg: 0.278
hr: 65
rbi: 147

##### data #####
$VAR1 = {
          'hr' => '65',
          'avg' => '0.278',
          'rbi' => '147'
        };

ちゃんとコメントが解釈されている。

Example 2.6. Mapping of Mappings

Mark McGwire: {hr: 65, avg: 0.278}
Sammy Sosa: {
    hr: 63,
    avg: 0.288
  }

これも問題なく解釈できた。

Example 2.8. Play by Play Feed from a Game

---
time: 20:03:20
player: Sammy Sosa
action: strike (miss)
...
---
time: 20:03:47
player: Sammy Sosa
action: grand slam
...
##### yaml #####
---
action: strike (miss)
player: Sammy Sosa
time: 20:03:20
---
action: grand slam
player: Sammy Sosa
time: 20:03:47

##### data #####
$VAR1 = {
          'time' => '20:03:20',
          'action' => 'strike (miss)',
          'player' => 'Sammy Sosa'
        };
$VAR2 = {
          'time' => '20:03:47',
          'action' => 'grand slam',
          'player' => 'Sammy Sosa'
        };

'...'を解釈できてる。

Example 2.9. Single Document with Two Comments

---
hr: # 1998 hr ranking
  - Mark McGwire
  - Sammy Sosa
rbi:
  # 1998 rbi ranking
  - Sammy Sosa
  - Ken Griffey

行頭に'#'が来ない場合のコメントも大丈夫。

Example 2.11. Mapping between Sequences

? - Detroit Tigers
  - Chicago cubs
:
  - 2001-07-23

? [ New York Yankees,
    Atlanta Braves ]
: [ 2001-07-02, 2001-08-12,
    2001-08-14 ]

問題ない。

##### yaml #####
---
ARRAY(0x8100088):
- 2001-07-23
ARRAY(0x8333344):
- 2001-07-02
- 2001-08-12
- 2001-08-14

##### data #####
$VAR1 = {
          'ARRAY(0x8100088)' => [
                                  '2001-07-23'
                                ],
          'ARRAY(0x8333344)' => [
                                  '2001-07-02',
                                  '2001-08-12',
                                  '2001-08-14'
                                ]
        };

Example 2.14. In the plain scalar, newlines become spaces

---
  Mark McGwire's
  year was crippled
  by a knee injury.
##### yaml #####
--- Mark McGwire's year was crippled by a knee injury.

##### data #####
$VAR1 = 'Mark McGwire\'s year was crippled by a knee injury.';

Plain Scalarも大丈夫。

Example 2.15. Folded newlines are preserved for "more indented" and blank lines

>
 Sammy Sosa completed another
 fine season with great stats.

   63 Home Runs
   0.288 Batting Average

 What a year!
##### yaml #####
--- "Sammy Sosa completed another fine season with great stats.\n\n  63 Home Run  \ 0.288 Batting Average\n\nWhat a year!\n"

##### data #####
$VAR1 = 'Sammy Sosa completed another fine season with great stats.

  63 Home Runs
  0.288 Batting Average

What a year!
';

Example 2.18. Multi-line Flow Scalars

plain:
  This unquoted scalar
  spans many lines.

quoted: "So does this
  quoted scalar.\n"
##### yaml #####
---
plain: This unquoted scalar spans many lines.
quoted: 'So does this quoted scalar.

'

##### data #####
$VAR1 = {
          'plain' => 'This unquoted scalar spans many lines.',
          'quoted' => 'So does this quoted scalar.
'
        };

Example 2.19-23 : Tagsが文字列として解釈されるのは変わらず、か。

Example 2.24. Global Tags

%TAG ! tag:clarkevans.com,2002:
--- !shape
  # Use the ! handle for presenting
  # tag:clarkevans.com,2002:circle
- !circle
  center: &ORIGIN {x: 73, y: 129}
  radius: 7
- !line
  start: *ORIGIN
  finish: { x: 89, y: 102 }
- !label
  start: *ORIGIN
  color: 0xFFEEBB
  text: Pretty vector drawing.

エラーになったが、指定されたハンドルがないといわれているのでまあわかる気がする。この辺どうしたものなんかは正直よくわかってない。

YAML::XS::Load Error: The problem:

    bad tag found for hash: 'tag:clarkevans.com,2002:circle'

was found at document: 1

Example 2.25. Unordered Sets

# sets are represented as a
# mapping where each key is
# associated with the empty string
--- !!set
? Mark McGwire
? Sammy Sosa
? Ken Griff

エラー

YAML::XS::Load Error: The problem:

    bad tag found for hash: 'tag:yaml.org,2002:set'

was found at document: 1

Example 2.26. Ordered Mappings

# ordered maps are represented as
# a sequence of mappings, with
# each mapping having one key
--- !!omap
- Mark McGwire: 65
- Sammy Sosa: 63
- Ken Griffy: 58

エラー

YAML::XS::Load Error: The problem:

    bad tag found for array: 'tag:yaml.org,2002:omap'

was found at document: 2