Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Should IO::YAML ignore leading comments?

by diotalevi (Canon)
on Sep 18, 2007 at 18:36 UTC ( #639724=perlquestion: print w/replies, xml ) Need Help??

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

I've got a very large YAML stream and it begins with a little bit of comments. When I tried using IO::YAML, I found that it treats my leading comments as a separate chunk. Is this a bug?

use IO::YAML; my $yaml_fh = IO::YAML->new( handle => \*DATA, mode => '<', auto_load => 0, ) or die "Can't open ...: $!"; while ( not $yaml_fh->eof ) { my $yaml = <$yaml_fh>; # Skip comment-only blocks. Is it a bug that this ever happens? next if /\A(?:^#[^\n]*\n)+\z/m; } __DATA__ # this is a comment about the 40MB of YAML documents to follow --- foo: 1 bar: 2

⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Replies are listed 'Best First'.
Re: Should IO::YAML ignore leading comments?
by dynamo (Chaplain) on Sep 18, 2007 at 19:17 UTC
    I don't think it's a bug. And it's not necessarily ignoring leading comments in that case - the "---" line is a document start indicator, and is optional. So you have an empty doc there with a comment, and then another doc w/ data. Try putting your comment below the "---"
    - dyn
    Update: I was wrong.

    The leading comment should NOT start a new document, this IS a bug.
    See the spec as it relates to comments. Also, see this example, where it says two documents rather than the four it would be if comments were their own documents.
    Despite this, in the interests of pragmatism, if you don't want to fix IO::YAML or wait for the author to do so, I'd still recommend putting the comment below the start indicator.
      Why were you wrong? Your explanation was what I would have thought of. Can you explain why it isn't so?



        I'm not 100% sure I understand your question, but basically, I am wrong about the above example being two docs (the first one empty, aside from the comment) because the current (v1.1) YAML spec says so, and well, being the spec and all, it trumps my opinion.

        The second link from my updated comment (figure 2.7), and most especially it's caption "_two_ documents in a stream" says it better than I could if I were to try to rephrase.

        If comments could be their own documents, that caption would say "four documents in a stream, two of which are empty but have comments".

        Hope that helps, - d

Re: Should IO::YAML ignore leading comments?
by Anonymous Monk on Sep 23, 2007 at 00:44 UTC
    Fixed in IO-YAML 0.07 (now on its way to CPAN). Thanks for reporting this!
      Oops, forgot to login. That was me (nkuitse).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://639724]
Approved by ikegami
Front-paged by ikegami
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2020-10-27 06:09 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (256 votes). Check out past polls.