in reply to YAML: reading a file w/ multiple docs in it, doc by doc
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 ); } } }
|
|---|