1 use strict; use warnings;
8 our @ISA = qw(Exporter YAML::Mo::Object);
9 our @EXPORT = qw(ynode);
13 if (ref($_[0]) eq 'HASH') {
14 $self = tied(%{$_[0]});
16 elsif (ref($_[0]) eq 'ARRAY') {
17 $self = tied(@{$_[0]});
19 elsif (ref(\$_[0]) eq 'GLOB') {
20 $self = tied(*{$_[0]});
25 return (ref($self) =~ /^yaml_/) ? $self : undef;
29 my ($class, $node, $tag) = @_;
31 $self->{NODE} = $node;
32 my (undef, $type) = YAML::Mo::Object->node_info($node);
33 $self->{KIND} = (not defined $type) ? 'scalar' :
34 ($type eq 'ARRAY') ? 'sequence' :
35 ($type eq 'HASH') ? 'mapping' :
36 $class->die("Can't create YAML::Node from '$type'");
37 tag($self, ($tag || ''));
38 if ($self->{KIND} eq 'scalar') {
39 yaml_scalar->new($self, $_[1]);
42 my $package = "yaml_" . $self->{KIND};
46 sub node { $_->{NODE} }
47 sub kind { $_->{KIND} }
49 my ($self, $value) = @_;
51 $self->{TAG} = YAML::Tag->new($value);
59 my ($self, $value) = @_;
61 $self->{KEYS} = $value;
69 #==============================================================================
72 @yaml_scalar::ISA = qw(YAML::Node);
75 my ($class, $self) = @_;
76 tie $_[2], $class, $self;
80 my ($class, $self) = @_;
91 my ($self, $value) = @_;
92 $self->{NODE} = $value
95 #==============================================================================
96 package yaml_sequence;
98 @yaml_sequence::ISA = qw(YAML::Node);
101 my ($class, $self) = @_;
103 tie @$new, $class, $self;
108 my ($class, $self) = @_;
114 scalar @{$self->{NODE}};
118 my ($self, $index) = @_;
119 $self->{NODE}[$index]
123 my ($self, $index, $value) = @_;
124 $self->{NODE}[$index] = $value
128 die "Not implemented yet"; # XXX
131 *STORESIZE = *POP = *PUSH = *SHIFT = *UNSHIFT = *SPLICE = *DELETE = *EXISTS =
132 *STORESIZE = *POP = *PUSH = *SHIFT = *UNSHIFT = *SPLICE = *DELETE = *EXISTS =
133 *undone; # XXX Must implement before release
135 #==============================================================================
136 package yaml_mapping;
138 @yaml_mapping::ISA = qw(YAML::Node);
141 my ($class, $self) = @_;
142 @{$self->{KEYS}} = sort keys %{$self->{NODE}};
144 tie %$new, $class, $self;
149 my ($class, $self) = @_;
154 my ($self, $key) = @_;
155 if (exists $self->{NODE}{$key}) {
156 return (grep {$_ eq $key} @{$self->{KEYS}})
157 ? $self->{NODE}{$key} : undef;
159 return $self->{HASH}{$key};
163 my ($self, $key, $value) = @_;
164 if (exists $self->{NODE}{$key}) {
165 $self->{NODE}{$key} = $value;
167 elsif (exists $self->{HASH}{$key}) {
168 $self->{HASH}{$key} = $value;
171 if (not grep {$_ eq $key} @{$self->{KEYS}}) {
172 push(@{$self->{KEYS}}, $key);
174 $self->{HASH}{$key} = $value;
180 my ($self, $key) = @_;
182 if (exists $self->{NODE}{$key}) {
183 $return = $self->{NODE}{$key};
185 elsif (exists $self->{HASH}{$key}) {
186 $return = delete $self->{NODE}{$key};
188 for (my $i = 0; $i < @{$self->{KEYS}}; $i++) {
189 if ($self->{KEYS}[$i] eq $key) {
190 splice(@{$self->{KEYS}}, $i, 1);
198 @{$self->{KEYS}} = ();
199 %{$self->{HASH}} = ();
210 $self->{KEYS}[++$self->{ITER}]
214 my ($self, $key) = @_;
215 exists $self->{NODE}{$key}