10 our @ISA = 'YAML::Base';
11 our @EXPORT = qw(ynode);
15 if (ref($_[0]) eq 'HASH') {
16 $self = tied(%{$_[0]});
18 elsif (ref($_[0]) eq 'ARRAY') {
19 $self = tied(@{$_[0]});
24 return (ref($self) =~ /^yaml_/) ? $self : undef;
28 my ($class, $node, $tag) = @_;
30 $self->{NODE} = $node;
31 my (undef, $type) = $class->node_info($node);
32 $self->{KIND} = (not defined $type) ? 'scalar' :
33 ($type eq 'ARRAY') ? 'sequence' :
34 ($type eq 'HASH') ? 'mapping' :
35 $class->die("Can't create YAML::Node from '$type'");
36 tag($self, ($tag || ''));
37 if ($self->{KIND} eq 'scalar') {
38 yaml_scalar->new($self, $_[1]);
41 my $package = "yaml_" . $self->{KIND};
45 sub node { $_->{NODE} }
46 sub kind { $_->{KIND} }
48 my ($self, $value) = @_;
50 $self->{TAG} = YAML::Tag->new($value);
58 my ($self, $value) = @_;
60 $self->{KEYS} = $value;
68 #==============================================================================
71 @yaml_scalar::ISA = qw(YAML::Node);
74 my ($class, $self) = @_;
75 tie $_[2], $class, $self;
79 my ($class, $self) = @_;
90 my ($self, $value) = @_;
91 $self->{NODE} = $value
94 #==============================================================================
95 package yaml_sequence;
97 @yaml_sequence::ISA = qw(YAML::Node);
100 my ($class, $self) = @_;
102 tie @$new, $class, $self;
107 my ($class, $self) = @_;
113 scalar @{$self->{NODE}};
117 my ($self, $index) = @_;
118 $self->{NODE}[$index]
122 my ($self, $index, $value) = @_;
123 $self->{NODE}[$index] = $value
127 die "Not implemented yet"; # XXX
130 *STORESIZE = *POP = *PUSH = *SHIFT = *UNSHIFT = *SPLICE = *DELETE = *EXISTS =
131 *STORESIZE = *POP = *PUSH = *SHIFT = *UNSHIFT = *SPLICE = *DELETE = *EXISTS =
132 *undone; # XXX Must implement before release
134 #==============================================================================
135 package yaml_mapping;
137 @yaml_mapping::ISA = qw(YAML::Node);
140 my ($class, $self) = @_;
141 @{$self->{KEYS}} = sort keys %{$self->{NODE}};
143 tie %$new, $class, $self;
148 my ($class, $self) = @_;
153 my ($self, $key) = @_;
154 if (exists $self->{NODE}{$key}) {
155 return (grep {$_ eq $key} @{$self->{KEYS}})
156 ? $self->{NODE}{$key} : undef;
158 return $self->{HASH}{$key};
162 my ($self, $key, $value) = @_;
163 if (exists $self->{NODE}{$key}) {
164 $self->{NODE}{$key} = $value;
166 elsif (exists $self->{HASH}{$key}) {
167 $self->{HASH}{$key} = $value;
170 if (not grep {$_ eq $key} @{$self->{KEYS}}) {
171 push(@{$self->{KEYS}}, $key);
173 $self->{HASH}{$key} = $value;
179 my ($self, $key) = @_;
181 if (exists $self->{NODE}{$key}) {
182 $return = $self->{NODE}{$key};
184 elsif (exists $self->{HASH}{$key}) {
185 $return = delete $self->{NODE}{$key};
187 for (my $i = 0; $i < @{$self->{KEYS}}; $i++) {
188 if ($self->{KEYS}[$i] eq $key) {
189 splice(@{$self->{KEYS}}, $i, 1);
197 @{$self->{KEYS}} = ();
198 %{$self->{HASH}} = ();
209 $self->{KEYS}[++$self->{ITER}]
213 my ($self, $key) = @_;
214 exists $self->{NODE}{$key}
223 YAML::Node - A generic data node that encapsulates YAML information
230 my $ynode = YAML::Node->new({}, 'ingerson.com/fruit');
231 %$ynode = qw(orange orange apple red grape green);
236 --- !ingerson.com/fruit
243 A generic node in YAML is similar to a plain hash, array, or scalar node
244 in Perl except that it must also keep track of its type. The type is a
245 URI called the YAML type tag.
247 YAML::Node is a class for generating and manipulating these containers.
248 A YAML node (or ynode) is a tied hash, array or scalar. In most ways it
249 behaves just like the plain thing. But you can assign and retrieve and
250 YAML type tag URI to it. For the hash flavor, you can also assign the
251 order that the keys will be retrieved in. By default a ynode will offer
252 its keys in the same order that they were assigned.
254 YAML::Node has a class method call new() that will return a ynode. You
255 pass it a regular node and an optional type tag. After that you can
256 use it like a normal Perl node, but when you YAML::Dump it, the magical
257 properties will be honored.
259 This is how you can control the sort order of hash keys during a YAML
260 serialization. By default, YAML sorts keys alphabetically. But notice
261 in the above example that the keys were Dumped in the same order they
264 YAML::Node exports a function called ynode(). This function returns the tied object so that you can call special methods on it like ->keys().
266 keys() works like this:
271 %$node = qw(orange orange apple red grape green);
272 $ynode = YAML::Node->new($node);
273 ynode($ynode)->keys(['grape', 'apple']);
282 It tells the ynode which keys and what order to use.
284 ynodes will play a very important role in how programs use YAML. They
285 are the foundation of how a Perl class can marshall the Loading and
286 Dumping of its objects.
288 The upcoming versions of YAML.pm will have much more information on this.
292 Ingy döt Net <ingy@cpan.org>
296 Copyright (c) 2006. Ingy döt Net. All rights reserved.
298 Copyright (c) 2002. Brian Ingerson. All rights reserved.
300 This program is free software; you can redistribute it and/or modify it
301 under the same terms as Perl itself.
303 See L<http://www.perl.com/perl/misc/Artistic.html>