Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: An Idiot's Guide to YAML

by clinton (Priest)
on Sep 04, 2007 at 09:16 UTC ( [id://636874]=note: print w/replies, xml ) Need Help??


in reply to Re: An Idiot's Guide to YAML
in thread An Idiot's Guide to YAML

It seems that the problem he had was not with the YAML config, but with reading the documentation. However, in answer to your question about why I would bother to use YAML, let me give you an example.

In my current project, I have a large amount of config, stored in a directory tree containing 59 separate YAML files, for ease of management.

I loaded and merged (with Config::Loader) all of this data, and then printed it out to two separate files, one with YAML::Syck and one with Data::Dumper. Both were set to use two spaces as an indent.

YAML Perl Perl > YAML ------------------------------------ Lines 3279 4150 27% Size 85 kB 139 kB 63% Size (no spaces) 49 kB 66 kB 35%
While some would argue that the last measure is irrelevant, because YAML requires white space, I would say that it IS relevant because, to be legible and maintainable, Perl also needs white space. Instead those numbers are a measure of the extra punctuation that is needed to represent your data - the more punctuation, the less visible is the stuff that actually matters.

In practice, I've added a lot of extra white space and comment lines etc, so that my actual config is 3350 lines and 105kB - still a good deal smaller than a compact Perl version. And the extra stuff is either unobtrusive (whitespace) or obviously not part of the actual data (comments). I would assume a similar growth in size if I were to do the same thing in Perl.

People complain about how difficult it is to edit YAML by hand, a sentiment I do not understand at all. The format is simple and easy to read. This example covers 95% of my configuration needs:

scalar: value array: - element 1 - element 2 - element 3 hash: key1: value1 key2: value2 key3: value3

There is a lot more you can do with it, such as referencing other data structures within the YAML, recursion, loading Perl code, here docs and multiline scalars. However, most of this is unnecessary for day to day usage.

The one thing that seem to trip people up is the whitespace. YAML doesn't accept tabs as whitespace and, in my opinion, this is a good thing. How wide is a tab? PBP recommends using spaces for indenting Perl code instead of tabs for this very reason. And quite frankly, if your Perl indentation is a mess, it's going to be difficult to maintain anyway. Clear indentation makes for legibility. Any editor that uses spaces for tabs and can do block indents will handle YAML with ease.

Opinions about YAML are strongly polarised, and I'm not suggesting that it is the One True Way. But there are a lot of nay-sayers that give YAML a bad reputation, and I think it is undeserved. It is a very flexible, easy, useful and widely available format, and in my opinion, deserves its place at the table.

Clint

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-03-19 08:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found