1 package Module::Build::YAML;
5 use vars qw($VERSION @EXPORT @EXPORT_OK);
8 @EXPORT_OK = qw(Dump Load DumpFile LoadFile);
12 my $class = ref($this) || $this;
19 shift if ($_[0] eq __PACKAGE__ || ref($_[0]) eq __PACKAGE__);
21 foreach my $item (@_) {
23 $yaml .= &_yaml_chunk("", $item);
29 shift if ($_[0] eq __PACKAGE__ || ref($_[0]) eq __PACKAGE__);
30 die "not yet implemented";
33 # This is basically copied out of YAML.pm and simplified a little.
35 shift if ($_[0] eq __PACKAGE__ || ref($_[0]) eq __PACKAGE__);
37 local $/ = "\n"; # reset special to "sane"
39 if ($filename =~ /^\s*(>{1,2})\s*(.*)$/) {
40 ($mode, $filename) = ($1, $2);
42 open my $OUT, "$mode $filename"
43 or die "Can't open $filename for writing: $!";
48 # This is basically copied out of YAML.pm and simplified a little.
50 shift if ($_[0] eq __PACKAGE__ || ref($_[0]) eq __PACKAGE__);
52 open my $IN, $filename
53 or die "Can't open $filename for reading: $!";
54 return Load(do { local $/; <$IN> });
59 my ($indent, $values) = @_;
61 my $ref = ref($values);
62 my ($value, @allkeys, %keyseen);
63 if (!$ref) { # a scalar
64 $yaml_chunk .= &_yaml_value($values) . "\n";
66 elsif ($ref eq "ARRAY") {
67 foreach $value (@$values) {
68 $yaml_chunk .= "$indent-";
71 $yaml_chunk .= " " . &_yaml_value($value) . "\n";
75 $yaml_chunk .= &_yaml_chunk("$indent ", $value);
79 else { # assume "HASH"
80 if ($values->{_order} && ref($values->{_order}) eq "ARRAY") {
81 @allkeys = @{$values->{_order}};
82 $values = { %$values };
83 delete $values->{_order};
85 push(@allkeys, sort keys %$values);
86 foreach my $key (@allkeys) {
87 next if (!defined $key || $key eq "" || $keyseen{$key});
89 $yaml_chunk .= "$indent$key:";
90 $value = $values->{$key};
93 $yaml_chunk .= " " . &_yaml_value($value) . "\n";
97 $yaml_chunk .= &_yaml_chunk("$indent ", $value);
105 # XXX doesn't handle embedded newlines
107 # undefs and empty strings will become empty strings
108 if (! defined $value || $value eq "") {
111 # allow simple scalars (without embedded quote chars) to be unquoted
112 elsif ($value !~ /["'\\]/) {
115 # strings without double-quotes get double-quoted
116 elsif ($value !~ /\"/) {
117 $value =~ s{\\}{\\\\}g;
120 # other strings get single-quoted
122 $value =~ s{([\\'])}{\\$1}g;
133 Module::Build::YAML - Provides just enough YAML support so that Module::Build works even if YAML.pm is not installed
137 use Module::Build::YAML;
143 Provides just enough YAML support so that Module::Build works even if YAML.pm is not installed.
145 Currently, this amounts to the ability to write META.yml files when "perl Build distmeta"
146 is executed via the Dump() and DumpFile() functions/methods.
150 Stephen Adkins <spadkins@gmail.com>
154 Copyright (c) 2006. Stephen Adkins. All rights reserved.
156 This program is free software; you can redistribute it and/or modify it
157 under the same terms as Perl itself.
159 See L<http://www.perl.com/perl/misc/Artistic.html>