1 package TAP::Parser::YAMLish::Writer;
9 my $ESCAPE_CHAR = qr{ [ \x00-\x1f \" ] }x;
12 z x01 x02 x03 x04 x05 x06 a
14 x10 x11 x12 x13 x14 x15 x16 x17
15 x18 x19 x1a e x1c x1d x1e x1f
18 # Create an empty TAP::Parser::YAMLish::Writer object
27 die "Need something to write"
31 my $out = shift || \*STDOUT;
33 die "Need a reference to something I can write to"
36 $self->{writer} = $self->_make_writer($out);
38 $self->_write_obj( '---', $obj );
41 delete $self->{writer};
50 if ( 'CODE' eq $ref ) {
53 elsif ( 'ARRAY' eq $ref ) {
54 return sub { push @$out, shift };
56 elsif ( 'SCALAR' eq $ref ) {
57 return sub { $$out .= shift() . "\n" };
59 elsif ( 'GLOB' eq $ref || 'IO::Handle' eq $ref ) {
60 return sub { print $out shift(), "\n" };
63 die "Can't write to $out";
68 $self->{writer}->( join '', @_ );
75 return '~' unless defined $val;
77 if ( $val =~ /$ESCAPE_CHAR/ ) {
80 $val =~ s/ ( [\x00-\x1f] ) / '\\' . $UNPRINTABLE[ ord($1) ] /gex;
84 if ( length($val) == 0 or $val =~ /\s/ ) {
96 my $indent = shift || 0;
98 if ( my $ref = ref $obj ) {
99 my $pad = ' ' x $indent;
100 if ( 'HASH' eq $ref ) {
102 $self->_put($prefix);
103 for my $key ( sort keys %$obj ) {
104 my $value = $obj->{$key};
106 $pad . $self->_enc_scalar($key) . ':',
112 $self->_put( $prefix, ' {}' );
115 elsif ( 'ARRAY' eq $ref ) {
117 $self->_put($prefix);
118 for my $value (@$obj) {
126 $self->_put( $prefix, ' []' );
130 die "Don't know how to enocde $ref";
134 $self->_put( $prefix, ' ', $self->_enc_scalar($obj) );
146 TAP::Parser::YAMLish::Writer - Write YAMLish data
154 use TAP::Parser::YAMLish::Writer;
159 three => [ 1, 2, 3 ],
162 my $yw = TAP::Parser::YAMLish::Writer->new;
164 # Write to an array...
165 $yw->write( $data, \@some_array );
167 # ...an open file handle...
168 $yw->write( $data, $some_file_handle );
171 $yw->write( $data, \$some_string );
174 $yw->write( $data, sub {
181 Encodes a scalar, hash reference or array reference as YAMLish.
189 my $writer = TAP::Parser::YAMLish::Writer->new;
191 The constructor C<new> creates and returns an empty
192 C<TAP::Parser::YAMLish::Writer> object.
194 =head2 Instance Methods
198 $writer->write($obj, $output );
200 Encode a scalar, hash reference or array reference as YAML.
204 print SOMEFILE "$line\n";
210 three => [ 1, 2, 3 ],
213 my $yw = TAP::Parser::YAMLish::Writer->new;
214 $yw->write( $data, $writer );
217 The C< $output > argument may be:
221 =item * a reference to a scalar to append YAML to
223 =item * the handle of an open file
225 =item * a reference to an array into which YAML will be pushed
227 =item * a code reference
231 If you supply a code reference the subroutine will be called once for
232 each line of output with the line as its only argument. Passed lines
233 will have no trailing newline.
237 Andy Armstrong, <andy@hexten.net>
241 L<YAML::Tiny>, L<YAML>, L<YAML::Syck>, L<Config::Tiny>, L<CSS::Tiny>,
242 L<http://use.perl.org/~Alias/journal/29427>
246 Copyright 2007 Andy Armstrong.
248 This program is free software; you can redistribute
249 it and/or modify it under the same terms as Perl itself.
251 The full text of the license can be found in the
252 LICENSE file included with this module.