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: $!";
43 binmode($OUT, ':utf8') if $] >= 5.008;
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 binmode($IN, ':utf8') if $] >= 5.008;
55 return Load(do { local $/; <$IN> });
60 my ($indent, $values) = @_;
62 my $ref = ref($values);
63 my ($value, @allkeys, %keyseen);
64 if (!$ref) { # a scalar
65 $yaml_chunk .= &_yaml_value($values) . "\n";
67 elsif ($ref eq "ARRAY") {
68 foreach $value (@$values) {
69 $yaml_chunk .= "$indent-";
72 $yaml_chunk .= " " . &_yaml_value($value) . "\n";
76 $yaml_chunk .= &_yaml_chunk("$indent ", $value);
80 else { # assume "HASH"
81 if ($values->{_order} && ref($values->{_order}) eq "ARRAY") {
82 @allkeys = @{$values->{_order}};
83 $values = { %$values };
84 delete $values->{_order};
86 push(@allkeys, sort keys %$values);
87 foreach my $key (@allkeys) {
88 next if (!defined $key || $key eq "" || $keyseen{$key});
90 $yaml_chunk .= "$indent$key:";
91 $value = $values->{$key};
94 $yaml_chunk .= " " . &_yaml_value($value) . "\n";
98 $yaml_chunk .= &_yaml_chunk("$indent ", $value);
108 return '~' if not defined $value;
110 # empty strings will become empty strings
111 return '""' if $value eq '';
113 # allow simple scalars (without embedded quote chars) to be unquoted
114 # (includes $%_+=-\;:,./)
115 return $value if $value !~ /["'`~\n!\@\#^\&\*\(\)\{\}\[\]\|<>\?]/;
117 # quote and escape strings with special values
119 if $value !~ /['`~\n!\#^\&\*\(\)\{\}\[\]\|\?]/; # nothing but " or @ or < or > (email addresses)
121 $value =~ s/\n/\\n/g; # handle embedded newlines
122 $value =~ s/"/\\"/g; # handle embedded quotes
132 Module::Build::YAML - Provides just enough YAML support so that Module::Build works even if YAML.pm is not installed
136 use Module::Build::YAML;
142 Provides just enough YAML support so that Module::Build works even if YAML.pm is not installed.
144 Currently, this amounts to the ability to write META.yml files when C<perl Build distmeta>
145 is executed via the Dump() and DumpFile() functions/methods.
149 Stephen Adkins <spadkins@gmail.com>
153 Copyright (c) 2006. Stephen Adkins. All rights reserved.
155 This program is free software; you can redistribute it and/or modify it
156 under the same terms as Perl itself.
158 See L<http://www.perl.com/perl/misc/Artistic.html>