1 package Module::Build::YAML;
4 use vars qw($VERSION @EXPORT @EXPORT_OK);
7 @EXPORT_OK = qw(Dump Load DumpFile LoadFile);
11 my $class = ref($this) || $this;
18 shift if ($_[0] eq __PACKAGE__ || ref($_[0]) eq __PACKAGE__);
20 foreach my $item (@_) {
22 $yaml .= &_yaml_chunk("", $item);
28 shift if ($_[0] eq __PACKAGE__ || ref($_[0]) eq __PACKAGE__);
29 die "not yet implemented";
32 # This is basically copied out of YAML.pm and simplified a little.
34 shift if ($_[0] eq __PACKAGE__ || ref($_[0]) eq __PACKAGE__);
36 local $/ = "\n"; # reset special to "sane"
38 if ($filename =~ /^\s*(>{1,2})\s*(.*)$/) {
39 ($mode, $filename) = ($1, $2);
41 open my $OUT, "$mode $filename"
42 or die "Can't open $filename for writing: $!";
47 # This is basically copied out of YAML.pm and simplified a little.
49 shift if ($_[0] eq __PACKAGE__ || ref($_[0]) eq __PACKAGE__);
51 open my $IN, $filename
52 or die "Can't open $filename for reading: $!";
53 return Load(do { local $/; <$IN> });
58 my ($indent, $values) = @_;
60 my $ref = ref($values);
61 my ($value, @allkeys, %keyseen);
62 if (!$ref) { # a scalar
63 $yaml_chunk .= &_yaml_value($values) . "\n";
65 elsif ($ref eq "ARRAY") {
66 foreach $value (@$values) {
67 $yaml_chunk .= "$indent-";
70 $yaml_chunk .= " " . &_yaml_value($value) . "\n";
74 $yaml_chunk .= &_yaml_chunk("$indent ", $value);
78 else { # assume "HASH"
79 if ($values->{_order} && ref($values->{_order}) eq "ARRAY") {
80 @allkeys = @{$values->{_order}};
81 $values = { %$values };
82 delete $values->{_order};
84 push(@allkeys, sort keys %$values);
85 foreach my $key (@allkeys) {
86 next if (!defined $key || $key eq "" || $keyseen{$key});
88 $yaml_chunk .= "$indent$key:";
89 $value = $values->{$key};
92 $yaml_chunk .= " " . &_yaml_value($value) . "\n";
96 $yaml_chunk .= &_yaml_chunk("$indent ", $value);
106 return '~' if not defined $value;
108 # empty strings will become empty strings
109 return '""' if $value eq '';
111 # allow simple scalars (without embedded quote chars) to be unquoted
112 # (includes $%_+=-\;:,./)
113 return $value if $value !~ /["'`~\n!\@\#^\&\*\(\)\{\}\[\]\|<>\?]/;
115 # quote and escape strings with special values
117 if $value !~ /['`~\n!\#^\&\*\(\)\{\}\[\]\|\?]/; # nothing but " or @ or < or > (email addresses)
119 $value =~ s/\n/\\n/g; # handle embedded newlines
120 $value =~ s/"/\\"/g; # handle embedded quotes
130 Module::Build::YAML - Provides just enough YAML support so that Module::Build works even if YAML.pm is not installed
134 use Module::Build::YAML;
140 Provides just enough YAML support so that Module::Build works even if YAML.pm is not installed.
142 Currently, this amounts to the ability to write META.yml files when "perl Build distmeta"
143 is executed via the Dump() and DumpFile() functions/methods.
147 Stephen Adkins <spadkins@gmail.com>
151 Copyright (c) 2006. Stephen Adkins. All rights reserved.
153 This program is free software; you can redistribute it and/or modify it
154 under the same terms as Perl itself.
156 See L<http://www.perl.com/perl/misc/Artistic.html>