aboutsummaryrefslogtreecommitdiffstats
path: root/Data-YAML-0.0.6/lib/Data/YAML/Writer.pm
blob: abce6703ddd9df9aefd4d60faccb51e991a6fa93 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package Data::YAML::Writer;

use strict;
use warnings;
use Carp;

use vars qw{$VERSION};

$VERSION = '0.0.6';

my $ESCAPE_CHAR = qr{ [\x00-\x1f\"] }x;

my @UNPRINTABLE = qw(
 z    x01  x02  x03  x04  x05  x06  a
 x08  t    n    v    f    r    x0e  x0f
 x10  x11  x12  x13  x14  x15  x16  x17
 x18  x19  x1a  e    x1c  x1d  x1e  x1f
);

# Create an empty Data::YAML::Writer object
sub new {
  my $class = shift;
  bless {}, $class;
}

sub write {
  my $self = shift;

  croak "Need something to write"
   unless @_;

  my $obj = shift;
  my $out = shift || \*STDOUT;

  croak "Need a reference to something I can write to"
   unless ref $out;

  $self->{writer} = $self->_make_writer( $out );

  $self->_write_obj( '---', $obj );
  $self->_put( '...' );

  delete $self->{writer};
}

sub _make_writer {
  my $self = shift;
  my $out  = shift;

  my $ref = ref $out;

  if ( 'CODE' eq $ref ) {
    return $out;
  }
  elsif ( 'ARRAY' eq $ref ) {
    return sub { push @$out, shift };
  }
  elsif ( 'SCALAR' eq $ref ) {
    return sub { $$out .= shift() . "\n" };
  }
  elsif ( 'GLOB' eq $ref || 'IO::Handle' eq $ref ) {
    return sub { print $out shift(), "\n" };
  }

  croak "Can't write to $out";
}

sub _put {
  my $self = shift;
  $self->{writer}->( join '', @_ );
}

sub _enc_scalar {
  my $self = shift;
  my $val  = shift;

  return '~' unless defined $val;

  if ( $val =~ /$ESCAPE_CHAR/ ) {
    $val =~ s/\\/\\\\/g;
    $val =~ s/"/\\"/g;
    $val =~ s/ ( [\x00-\x1f] ) / '\\' . $UNPRINTABLE[ ord($1) ] /gex;
    return qq{"$val"};
  }

  if ( length( $val ) == 0 or $val =~ /\s/ ) {
    $val =~ s/'/''/;
    return "'$val'";
  }

  return $val;
}

sub _write_obj {
  my $self   = shift;
  my $prefix = shift;
  my $obj    = shift;
  my $indent = shift || 0;

  if ( my $ref = ref $obj ) {
    my $pad = '  ' x $indent;
    $self->_put( $prefix );
    if ( 'HASH' eq $ref ) {
      for my $key ( sort keys %$obj ) {
        my $value = $obj->{$key};
        $self->_write_obj( $pad . $self->_enc_scalar( $key ) . ':',
          $value, $indent + 1 );
      }
    }
    elsif ( 'ARRAY' eq $ref ) {
      for my $value ( @$obj ) {
        $self->_write_obj( $pad . '-', $value, $indent + 1 );
      }
    }
    else {
      croak "Don't know how to encode $ref";
    }
  }
  else {
    $self->_put( $prefix, ' ', $self->_enc_scalar( $obj ) );
  }
}

1;

__END__


=head1 NAME

Data::YAML::Writer - Easy YAML serialisation

=head1 VERSION

This document describes Data::YAML::Writer version 0.0.6

=head1 SYNOPSIS
    
    use Data::YAML::Writer;
    
    my $data = {
        one => 1,
        two => 2,
        three => [ 1, 2, 3 ],
    };
    
    my $yw = Data::YAML::Writer->new;
    
    # Write to an array...
    $yw->write( $data, \@some_array );
    
    # ...an open file handle...
    $yw->write( $data, $some_file_handle );
    
    # ...a string ...
    $yw->write( $data, \$some_string );
    
    # ...or a closure
    $yw->write( $data, sub {
        my $line = shift;
        print "$line\n";
    } );


=head1 DESCRIPTION

Encodes a scalar, hash reference or array reference as YAML.

In the spirit of L<YAML::Tiny> this is a lightweight, dependency-free
YAML writer. While C<YAML::Tiny> is designed principally for working
with configuration files C<Data::YAML> concentrates on the transparent
round-tripping of YAML serialized Perl data structures.

The syntax produced by C<Data::YAML::Writer> is a subset of YAML.
Specifically it is the same subset of YAML that L<Data::YAML::Reader>
consumes. See L<Data::YAML> for more information.

=head1 INTERFACE

=over

=item C<< new >>

The constructor C<new> creates and returns an empty C<Data::YAML::Writer> object.

=item C<< write( $obj, $output ) >>

Encode a scalar, hash reference or array reference as YAML.

    my $writer = sub {
        my $line = shift;
        print SOMEFILE "$line\n";
    };
    
    my $data = {
        one => 1,
        two => 2,
        three => [ 1, 2, 3 ],
    };
    
    my $yw = Data::YAML::Writer->new;
    $yw->write( $data, $writer );


The C< $output > argument may be

=over

=item * a reference to a scalar to append YAML to

=item * the handle of an open file

=item * a reference to an array into which YAML will be pushed

=item * a code reference

=back

If you supply a code reference the subroutine will be called once for
each line of output with the line as its only argument. Passed lines
will have no trailing newline.

=back

=head1 BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests to
C<data-yaml@rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org>.

=head1 SEE ALSO

L<YAML::Tiny>, L<YAML>, L<YAML::Syck>, L<Config::Tiny>, L<CSS::Tiny>

=head1 AUTHOR

Andy Armstrong  C<< <andy@hexten.net> >>

=head1 LICENCE AND COPYRIGHT

Copyright (c) 2007, Andy Armstrong C<< <andy@hexten.net> >>. All rights reserved.

This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlartistic>.

=head1 DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.