Good Things pt 2- YAML

February 9, 2007

I got acquinted with YAML during my short run-in with Ruby on Rails (more on this some other day, hopefully). Their official description is:

YAML™ (rhymes with “camel”) is a straightforward machine parsable data serialization format designed for human readability and interaction with scripting languages such as Perl and Python.

Absolutely brilliant stuff. What it gives you, is a simple (no joke!) portable data format that’s truly human-readable (indentation, baby!) and can easily be parsed into virtually any programming language’s native data structure.

For example, in PHP (via the excellent Spyc library), this is how my webapp’s DB settings would look like:

database:

    host: localhost

    name: collective_development

    username: name

    password: passwd

    type: mysql

    charset: utf-8

Then you just do

include 'lib/spyc.php5';

$c = Spyc::YAMLLoad( 'lib/collective.yml' );

And your whole configuration is accessible in a PHP array:

$link = mysql_connect( $c['database']['host'], $c['database']['username'], $c['database']['password'] );