1 package Module::Build::YAML;
5 $VERSION = '0.2808_01';
6 $VERSION = eval $VERSION;
8 use vars qw($VERSION @EXPORT @EXPORT_OK);
11 @EXPORT_OK = qw(Dump Load DumpFile LoadFile);
15 my $class = ref($this) || $this;
22 shift if ($_[0] eq __PACKAGE__ || ref($_[0]) eq __PACKAGE__);
24 foreach my $item (@_) {
26 $yaml .= &_yaml_chunk("", $item);
32 shift if ($_[0] eq __PACKAGE__ || ref($_[0]) eq __PACKAGE__);
33 die "not yet implemented";
36 # This is basically copied out of YAML.pm and simplified a little.
38 shift if ($_[0] eq __PACKAGE__ || ref($_[0]) eq __PACKAGE__);
40 local $/ = "\n"; # reset special to "sane"
42 if ($filename =~ /^\s*(>{1,2})\s*(.*)$/) {
43 ($mode, $filename) = ($1, $2);
45 open my $OUT, "$mode $filename"
46 or die "Can't open $filename for writing: $!";
51 # This is basically copied out of YAML.pm and simplified a little.
53 shift if ($_[0] eq __PACKAGE__ || ref($_[0]) eq __PACKAGE__);
55 open my $IN, $filename
56 or die "Can't open $filename for reading: $!";
57 return Load(do { local $/; <$IN> });
62 my ($indent, $values) = @_;
64 my $ref = ref($values);
65 my ($value, @allkeys, %keyseen);
66 if (!$ref) { # a scalar
67 $yaml_chunk .= &_yaml_value($values) . "\n";
69 elsif ($ref eq "ARRAY") {
70 foreach $value (@$values) {
71 $yaml_chunk .= "$indent-";
74 $yaml_chunk .= " " . &_yaml_value($value) . "\n";
78 $yaml_chunk .= &_yaml_chunk("$indent ", $value);
82 else { # assume "HASH"
83 if ($values->{_order} && ref($values->{_order}) eq "ARRAY") {
84 @allkeys = @{$values->{_order}};
85 $values = { %$values };
86 delete $values->{_order};
88 push(@allkeys, sort keys %$values);
89 foreach my $key (@allkeys) {
90 next if (!defined $key || $key eq "" || $keyseen{$key});
92 $yaml_chunk .= "$indent$key:";
93 $value = $values->{$key};
96 $yaml_chunk .= " " . &_yaml_value($value) . "\n";
100 $yaml_chunk .= &_yaml_chunk("$indent ", $value);
110 return '~' if not defined $value;
112 # empty strings will become empty strings
113 return '""' if $value eq '';
115 # allow simple scalars (without embedded quote chars) to be unquoted
116 # (includes $%_+=-\;:,./)
117 return $value if $value !~ /["'`~\n!\@\#^\&\*\(\)\{\}\[\]\|<>\?]/;
119 # quote and escape strings with special values
121 if $value !~ /['`~\n!\#^\&\*\(\)\{\}\[\]\|\?]/; # nothing but " or @ or < or > (email addresses)
123 $value =~ s/\n/\\n/g; # handle embedded newlines
124 $value =~ s/"/\\"/g; # handle embedded quotes
134 Module::Build::YAML - Provides just enough YAML support so that Module::Build works even if YAML.pm is not installed
138 use Module::Build::YAML;
144 Provides just enough YAML support so that Module::Build works even if YAML.pm is not installed.
146 Currently, this amounts to the ability to write META.yml files when "perl Build distmeta"
147 is executed via the Dump() and DumpFile() functions/methods.
151 Stephen Adkins <spadkins@gmail.com>
155 Copyright (c) 2006. Stephen Adkins. All rights reserved.
157 This program is free software; you can redistribute it and/or modify it
158 under the same terms as Perl itself.
160 See L<http://www.perl.com/perl/misc/Artistic.html>