1package YAML::Syck;
2# See documentation after the __END__ mark.
3
4use strict;
5use vars qw(
6    @ISA @EXPORT $VERSION
7    $Headless $SortKeys $SingleQuote
8    $ImplicitBinary $ImplicitTyping $ImplicitUnicode
9    $UseCode $LoadCode $DumpCode
10    $DeparseObject
11);
12use 5.00307;
13use Exporter;
14
15BEGIN {
16    $VERSION = '1.07';
17    @EXPORT  = qw( Dump Load DumpFile LoadFile );
18    @ISA     = qw( Exporter );
19
20    $SortKeys = 1;
21
22    local $@;
23    eval {
24        require XSLoader;
25        XSLoader::load(__PACKAGE__, $VERSION);
26        1;
27    } or do {
28        require DynaLoader;
29        push @ISA, 'DynaLoader';
30        __PACKAGE__->bootstrap($VERSION);
31    };
32
33}
34
35use constant QR_MAP => {
36    ''   => sub { qr{$_[0]}     },
37    x    => sub { qr{$_[0]}x    },
38    i    => sub { qr{$_[0]}i    },
39    s    => sub { qr{$_[0]}s    },
40    m    => sub { qr{$_[0]}m    },
41    ix   => sub { qr{$_[0]}ix   },
42    sx   => sub { qr{$_[0]}sx   },
43    mx   => sub { qr{$_[0]}mx   },
44    si   => sub { qr{$_[0]}si   },
45    mi   => sub { qr{$_[0]}mi   },
46    ms   => sub { qr{$_[0]}sm   },
47    six  => sub { qr{$_[0]}six  },
48    mix  => sub { qr{$_[0]}mix  },
49    msx  => sub { qr{$_[0]}msx  },
50    msi  => sub { qr{$_[0]}msi  },
51    msix => sub { qr{$_[0]}msix },
52};
53
54sub __qr_helper {
55    if ($_[0] =~ /\A  \(\?  ([ixsm]*)  (?:-  (?:[ixsm]*))?  : (.*) \)  \z/x) {
56        my $sub = QR_MAP()->{$1} || QR_MAP()->{''};
57        &$sub($2);
58    }
59    else {
60        qr/$_[0]/;
61    }
62}
63
64sub Dump {
65    $#_ ? join('', map { YAML::Syck::DumpYAML($_) } @_)
66        : YAML::Syck::DumpYAML($_[0]);
67}
68
69sub Load {
70    if (wantarray) {
71        my ($rv) = YAML::Syck::LoadYAML($_[0]);
72        @{$rv};
73    }
74    else {
75        YAML::Syck::LoadYAML($_[0]);
76    }
77}
78
79# NOTE. The code below (_is_openhandle) avoids to require/load
80# Scalar::Util unless it is given a ref or glob
81# as an argument. That is purposeful, so to avoid
82# the need for this dependency unless strictly necessary.
83# If that was not the case, Scalar::Util::openhandle could
84# be used directly.
85
86sub _is_openhandle {
87    my $h = shift;
88    if ( ref($h) || ref(\$h) eq 'GLOB' ) {
89        require Scalar::Util;
90        return Scalar::Util::openhandle($h);
91    } else {
92        return undef;
93    }
94}
95
96sub DumpFile {
97    my $file = shift;
98    if ( _is_openhandle($file) ) {
99        if ($#_) {
100            print {$file} YAML::Syck::DumpYAML($_) for @_;
101        }
102        else {
103            print {$file} YAML::Syck::DumpYAML($_[0]);
104        }
105    }
106    else {
107        local *FH;
108        open FH, "> $file" or die "Cannot write to $file: $!";
109        if ($#_) {
110            print FH YAML::Syck::DumpYAML($_) for @_;
111        }
112        else {
113            print FH YAML::Syck::DumpYAML($_[0]);
114        }
115        close FH;
116    }
117}
118
119sub LoadFile {
120    my $file = shift;
121    if ( _is_openhandle($file) ) {
122        Load(do { local $/; <$file> });
123    }
124    else {
125        local *FH;
126        open FH, "< $file" or die "Cannot read from $file: $!";
127        Load(do { local $/; <FH> });
128    }
129}
130
1311;
132
133__END__
134=pod
135
136=head1 NAME
137
138YAML::Syck - Fast, lightweight YAML loader and dumper
139
140=head1 VERSION
141
142This document describes version 1.07 of YAML::Syck, released April 25, 2009.
143
144=head1 SYNOPSIS
145
146    use YAML::Syck;
147
148    # Set this for interoperability with other YAML/Syck bindings:
149    # e.g. Load('Yes') becomes 1 and Load('No') becomes ''.
150    $YAML::Syck::ImplicitTyping = 1;
151
152    $data = Load($yaml);
153    $yaml = Dump($data);
154
155    # $file can be an IO object, or a filename
156    $data = LoadFile($file);
157    DumpFile($file, $data);
158
159    # A string with multiple YAML streams in it
160    $yaml = Dump(@data);
161    @data = Load($yaml);
162
163=head1 DESCRIPTION
164
165This module provides a Perl interface to the B<libsyck> data serialization
166library.  It exports the C<Dump> and C<Load> functions for converting
167Perl data structures to YAML strings, and the other way around.
168
169B<NOTE>: If you are working with other language's YAML/Syck bindings
170(such as Ruby), please set C<$YAML::Syck::ImplicitTyping> to C<1> before
171calling the C<Load>/C<Dump> functions.  The default setting is for
172preserving backward-compatibility with C<YAML.pm>.
173
174=head1 FLAGS
175
176=head2 $YAML::Syck::Headless
177
178Defaults to false.  Setting this to a true value will make C<Dump> omit the
179leading C<---\n> marker.
180
181=head2 $YAML::Syck::SortKeys
182
183Defaults to false.  Setting this to a true value will make C<Dump> sort
184hash keys.
185
186=head2 $YAML::Syck::SingleQuote
187
188Defaults to false.  Setting this to a true value will make C<Dump> always emit
189single quotes instead of bare strings.
190
191=head2 $YAML::Syck::ImplicitTyping
192
193Defaults to false.  Setting this to a true value will make C<Load> recognize
194various implicit types in YAML, such as unquoted C<true>, C<false>, as well as
195integers and floating-point numbers.  Otherwise, only C<~> is recognized to
196be C<undef>.
197
198=head2 $YAML::Syck::ImplicitUnicode
199
200Defaults to false.  For Perl 5.8.0 or later, setting this to a true value will
201make C<Load> set Unicode flag on for every string that contains valid UTF8
202sequences, and make C<Dump> return a unicode string.
203
204Regardless of this flag, Unicode strings are dumped verbatim without escaping;
205byte strings with high-bit set will be dumped with backslash escaping.
206
207However, because YAML does not distinguish between these two kinds of strings,
208so this flag will affect loading of both variants of strings.
209
210=head2 $YAML::Syck::ImplicitBinary
211
212Defaults to false.  For Perl 5.8.0 or later, setting this to a true value will
213make C<Dump> generate Base64-encoded C<!!binary> data for all non-Unicode
214scalars containing high-bit bytes.
215
216=head2 $YAML::Syck::UseCode / $YAML::Syck::LoadCode / $YAML::Syck::DumpCode
217
218These flags control whether or not to try and eval/deparse perl source code;
219each of them defaults to false.
220
221Setting C<$YAML::Syck::UseCode> to a true value is equivalent to setting
222both C<$YAML::Syck::LoadCode> and C<$YAML::Syck::DumpCode> to true.
223
224=head1 BUGS
225
226Dumping Glob/IO values does not work yet.
227
228=head1 CAVEATS
229
230This module implements the YAML 1.0 spec.  To deal with data in YAML 1.1,
231please use the C<YAML::XS> module instead.
232
233The current implementation bundles libsyck source code; if your system has a
234site-wide shared libsyck, it will I<not> be used.
235
236Tag names such as C<!!perl/hash:Foo> is blessed into the package C<Foo>, but
237the C<!hs/foo> and C<!!hs/Foo> tags are blessed into C<hs::Foo>.  Note that
238this holds true even if the tag contains non-word characters; for example,
239C<!haskell.org/Foo> is blessed into C<haskell.org::Foo>.  Please use
240L<Class::Rebless> to cast it into other user-defined packages.
241
242=head1 SEE ALSO
243
244L<YAML>, L<JSON::Syck>
245
246L<http://www.yaml.org/>
247
248=head1 AUTHORS
249
250Audrey Tang E<lt>cpan@audreyt.orgE<gt>
251
252=head1 COPYRIGHT
253
254Copyright 2005-2009 by Audrey Tang E<lt>cpan@audreyt.orgE<gt>.
255
256This software is released under the MIT license cited below.
257
258The F<libsyck> code bundled with this library is released by
259"why the lucky stiff", under a BSD-style license.  See the F<COPYING>
260file for details.
261
262=head2 The "MIT" License
263
264Permission is hereby granted, free of charge, to any person obtaining a copy
265of this software and associated documentation files (the "Software"), to deal
266in the Software without restriction, including without limitation the rights
267to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
268copies of the Software, and to permit persons to whom the Software is
269furnished to do so, subject to the following conditions:
270
271The above copyright notice and this permission notice shall be included in
272all copies or substantial portions of the Software.
273
274THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
275OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
276FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
277THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
278LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
279FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
280DEALINGS IN THE SOFTWARE.
281
282=cut
283