anarres: (Default)
2010-08-11 07:59 am
Entry tags:

YAML

YAML is a "human-readable data serialization format", according to Wikipedia. Apparently it's terribly flexible and powerful. But I just want to make a simple configuration file to use with a Perl script. I worked out how to do this with help from Stackoverflow (I love Stackoverflow). Super-simple example:

test.yaml:

---
image_width: 1000
show_values: 0


test.pl:

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

use YAML qw(LoadFile);

my $settings = LoadFile('test.yaml');
say "The image width is ", $settings->{image_width};


You can also go backwards: starting from the Perl code version of the configuration data you want, you can generate the YAML file:

make_yaml.pl

use strict;
use warnings;

use YAML;

my %settings = (
foo => 1,
bar => [qw/one two three/],
);

print Dump(\%settings);


Hmmm, funny how the Stackoverflow people assume I'm a "he" though.