water has asked for the wisdom of the Perl Monks concerning the following question:

Yaml supports LoadFile, which reads all docs in a file.

Is there a YAML method to read a file doc by doc, rather than whole file at once?

Or would one do this manually, reading the file into a buffer until a doc is complete, then passing the chunk off to yaml?

thanks

water

  • Comment on YAML: reading a file w/ multiple docs in it, doc by doc

Replies are listed 'Best First'.
Re: YAML: reading a file w/ multiple docs in it, doc by doc
by diotalevi (Canon) on Nov 11, 2004 at 17:08 UTC

    This is not tested.

    my $reader = YAML::ReadStream( $file_handle ); while ( my $document = $reader->() ) { ... } sub YAML::ReadStream { my $handle = shift; my $buffer; return sub { if ( eof $handle ) { undef $buffer; return undef; } else { return YAML::_ReadOneDocument( $handle, \ $buffer ); } }; } sub YAML::_ReadOneDocument { my $handle = shift; my $buffer = shift; while ( 1 ) { if ( eof $handle ) { return YAML::Load( $$buffer ); } read $handle, $$buffer, 8192, length $$buffer; my $yaml_header_position = index $$buffer, "\n---", length( "- +--" ); if ( $yaml_header_position >= 0 ) { my $doc = substr $$buffer, 0, $yaml_header_position, ''; return YAML::Load( $doc ); } } }