Subject: | [PATCH] Support for tied hash (thus ordered data) |
Hi.
This patch allows to use a provided module name to create tied hashes and use them when
loading a yaml string. That way, the data order can be preserved. The pach includes more POD
doc.
Subject: | YAML-Tiny-1.25_TieHash.patch |
Only in YAML-Tiny-1.25: Makefile
Only in YAML-Tiny-1.25: blib
diff -ru YAML-Tiny-1.25_old/lib/YAML/Tiny.pm YAML-Tiny-1.25/lib/YAML/Tiny.pm
--- YAML-Tiny-1.25_old/lib/YAML/Tiny.pm 2008-01-28 16:39:05.000000000 +0700
+++ YAML-Tiny-1.25/lib/YAML/Tiny.pm 2008-01-28 18:45:54.000000000 +0700
@@ -14,6 +14,18 @@
};
}
+our $TieHashModule = undef;
+
+# used to create a hashref, possibly using hash ties (for instance, to get ordered hash)
+sub _create_hashref {
+ $TieHashModule or return {};
+ eval ("require $TieHashModule");
+ $@ and die "YAML::Tiny failed requiring '$TieHashModule', specified to be used as a hash tie";
+ my %hash;
+ tie %hash, $TieHashModule;
+ return \%hash;
+}
+
my $ESCAPE_CHAR = '[\\x00-\\x08\\x0b-\\x0d\\x0e-\\x1f\"\n]';
# Escapes for unprintable characters
@@ -100,7 +112,7 @@
} elsif ( $lines[0] =~ /^(\s*)\w/ ) {
# A hash at the root
- my $document = { };
+ my $document = _create_hashref();
push @$self, $document;
$self->_read_hash( $document, [ length($1) ], \@lines );
@@ -196,7 +208,7 @@
# Inline nested hash
my $indent2 = length("$1");
$lines->[0] =~ s/-/ /;
- push @$array, { };
+ push @$array, _create_hashref();
$self->_read_hash( $array->[-1], [ @$indent, $indent2 ], $lines );
} elsif ( $lines->[0] =~ /^\s*\-(\s*)(.+?)\s*$/ ) {
@@ -222,7 +234,7 @@
}
} elsif ( $lines->[0] =~ /^(\s*)\w/ ) {
- push @$array, { };
+ push @$array, _create_hashref();
$self->_read_hash( $array->[-1], [ @$indent, length("$1") ], $lines );
} else {
@@ -279,7 +291,7 @@
# Null hash entry
$hash->{$key} = undef;
} else {
- $hash->{$key} = {};
+ $hash->{$key} = _create_hashref();
$self->_read_hash( $hash->{$key}, [ @$indent, length($1) ], $lines );
}
}
@@ -411,7 +423,7 @@
die "YAML::Tiny does not support circular references";
}
my @lines = ();
- foreach my $name ( sort keys %$hash ) {
+ foreach my $name ( $TieHashModule ? sort keys %$hash : keys %$hash ) {
my $el = $hash->{$name};
my $line = (' ' x $indent) . "$name:";
my $type = ref $el;
@@ -608,8 +620,8 @@
sense of.
L<YAML::Tiny> does not generate comments, it won't necesarily preserve the
-order of your hashes, and it will normalise if reading in and writing out
-again.
+order of your hashes (except if you use C<YAML::Tiny::TieHashModule>, see
+below), and it will normalise if reading in and writing out again.
It only supports a very basic subset of the full YAML specification.
@@ -992,6 +1004,19 @@
Reads the YAML stream from a file instead of a string.
+=head1 VARIABLES
+
+=head2 C<YAML::Tiny::TieHashModule>
+
+This module variable can be use to specify a module to be used to ie hashes
+against, thus providing more features to the hashes. For instance, using
+Tie::IxHash, Tie::Hash::Indexed or Ties::Hash::Sorted, the order of YAML data
+can be preserved, or sorted. The following example rounds-trip better :
+
+ local $YAML::Tiny::TieHashModule = 'Tie::IxHash';
+ my ($structure) = YAML::Tiny::LoadFile($filename);
+ my $output = YAML::Tiny::DumpFile($filename);
+
=head1 SUPPORT
Bugs should be reported via the CPAN bug tracker at
Only in YAML-Tiny-1.25: pm_to_blib