Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

YAML::Syck serializing Dump into single line

by cLive ;-) (Prior)
on Jan 25, 2007 at 07:24 UTC ( [id://596422]=perlquestion: print w/replies, xml ) Need Help??

cLive ;-) has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to work out if there's a YAML::Syck equivalent of:

$Data::Dumper::Indent=0;

So that dumped data can be serialized into one line. I've tried googling on the obvious terms, but only find references to how scalars can be folded to make them more readable.

I don't suppose there are any YAML heads out there who can give me a definitive answer on this one? The syck documentation is pretty sparse.

cLive ;-)

Replies are listed 'Best First'.
Re: YAML::Syck serializing Dump into single line
by demerphq (Chancellor) on Jan 25, 2007 at 10:52 UTC

    YAML is a whitespace sensitive format. Therefore expecting it to serilize to a single line is contradictory to its inherent nature.

    ---
    $world=~s/war/peace/g

      What cLive ;-) probably meant was for some option to make the YAML::Syck dumper spit inline sequences and maps, as in

      { a : [ 1, 2, 3], b : { b_a: "a\n" } }
      instead of the usual multi-line representation (which uses a lot of indentation indeed).
      --- a: - 1 - 2 - 3 b: b_a: "a\n"

      I am not sure of what exactly syck can do, but it is very possible that even if there's such option in the underlying library, it is not currently exposed by the binding code implemented by YAML::Syck. The module is very immature yet, but fast as hell (for things it can grok).

        Perhaps JSON::Syck (which uses libsyck to spit out more JSON-friendly YAML) may be more what he's looking for.

        $ perl -MJSON::Syck=Dump -le 'print JSON::Syck::Dump( { a => { b => "c +", d => [qw/1 2 3/] } } )' {"a":{"b":"c","d":["1","2","3"]}}

        "The module is very immature yet, but fast as hell (for things it can grok)."

        Indeed! I benchmarked it against reading/writing a hashref of arrayref of hashrefs against XML::Simple, and it created files three times faster, and parsed them nine times faster. Unfortunately, the context I'm currently working in involves one line of data transfer only, so I need to work around the new lines if I'm going to work on integrating it.

        I think I'll have to examine the JSON::Syck implementation when I finally get round to trying to implement this. I like YAML because, unlike XML, it actually makes sense to me :)

Re: YAML::Syck serializing Dump into single line
by ambrus (Abbot) on Jan 26, 2007 at 11:03 UTC

    You could dump the data to a string and then escape the newliens in it with a regular expression. Then, the other side could undo the escaping and then parse with yaml.

    For example, escape newlines with something like this (untested):

    s/\x1f/\x1f_/g; s/\n/\x1fj/g;
    and undo it with
    s/\x1fj/\n/g; s/\x1f_/\x1f/g;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://596422]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-26 02:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found