Data-YAML

 view release on metacpan or  search on metacpan

lib/Data/YAML/Reader.pm  view on Meta::CPAN

  my ( $self, $line, $limit ) = @_;

  my $indent;
  my $hash = {};

  while ( 1 ) {
    croak "Badly formed hash line: '$line'"
     unless $line =~ $HASH_LINE;

    my ( $key, $value ) = ( $self->_read_scalar( $1 ), $2 );
    $self->_next;

    if ( defined $value ) {
      $hash->{$key} = $self->_read_scalar( $value );
    }
    else {
      $hash->{$key} = $self->_read_nested;
    }

    ( $line, $indent ) = $self->_peek;
    last if $indent < $limit || !defined $line || $line =~ $IS_END_YAML;
  }

  return $hash;
}

1;

__END__

=head1 NAME

Data::YAML::Reader - Parse YAML created by Data::YAML::Writer

=head1 VERSION

This document describes Data::YAML::Reader version 0.0.7

=head1 SYNOPSIS

    use Data::YAML::Reader;

    my $yr = Data::YAML::Reader->new;
    
    # Read from an array...
    my $from_array = $yr->read( \@some_array );
    
    # ...an open file handle...
    my $from_handle = $yr->read( $some_file );
    
    # ...a string containing YAML...
    my $from_string = $yr->read( $some_string );
    
    # ...or a closure
    my $from_code = $yr->read( sub { return get_next_line() } );

=head1 DESCRIPTION

In the spirit of L<YAML::Tiny> this is a lightweight, dependency-free
YAML reader. 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 accepted by C<Data::YAML::Reader> is a subset of YAML.
Specifically it is the same subset of YAML that L<Data::YAML::Writer>
produces. See L<Data::YAML> for more information.

=head1 INTERFACE

=over

=item C<< new >>

Creates and returns an empty C<Data::YAML::Reader> object. No options may be passed.

=item C<< read( $source ) >>

Read YAML and return the data structure it represents. The YAML data may be supplied by a

=over

=item * scalar string containing YAML source

=item * the handle of an open file

=item * a reference to an array of lines

=item * a code reference

=back

In the case of a code reference a subroutine (most likely a closure)
that returns successive lines of YAML must be supplied. Lines should
have no trailing newline. When the YAML is exhausted the subroutine must
return undef.

Returns the data structure (specifically either a scalar, hash ref or
array ref) that results from decoding the YAML.

=item C<< get_raw >>

Return the raw YAML source from the most recent C<read>.

=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> >>



( run in 0.663 second using v1.01-cache-2.11-cpan-39bf76dae61 )