')

How to Load YAML Config Files

Config files are used by programmers to store local variables as it's usually better to update a config file than to edit source code. YAML is a popular data serialization language that's easy to read and can serialize the common Perl variables (scalars, arrays and hashes). This article describes how to read a YAML config file in Perl and access the config file's variables.

The config file

YAML defines different types of data collections that can be used to serialize (represent or store) Perl variables. Let's define a YAML config file to store some local email data. The config.yaml file could look like this:

---
emailName: David
emailAddresses: 
    - sillymoos@cpan.org
    - perltricks.com@gmail.com
credentials:
    username: sillymoose
    password: itsasecret

Let's walkthrough config.yaml: the config file starts with three hyphens ("---") to signify the start of the document, "emailName": is a scalar mapping with the value "David", "emailAddresses" is a sequence of email addresses and "credentials" is mapping of scalar mappings for the email username and password.

Load the config file

Perl's YAML::XS module provides a "LoadFile" subroutine that can be used to read any YAML file into a scalar variable. This script loads the "config.yaml" config file and prints it using Data::Dumper:

use strict;
use warnings;
use YAML::XS qw/LoadFile/;
use Data::Dumper;
    
my $config = LoadFile('config.yaml');

print Dumper($config);

If we run this script we get the following results:

$VAR1 = {
          'emailName' => 'David',
          'credentials' => {
                           'password' => 'itsasecret',
                           'username' => 'sillymoose'
                         },
          'emailAddresses' => [
                              'sillymoos@cpan.org',
                              'perltricks.com@gmail.com'
                            ]
        };

These results show that config.yaml has been read into $config as a hash reference with three keys: "emailName", "credentials" and "emailAddresses".

Accessing the config data

So far the script reads the contents of config.yaml into $config. To access the config data we need to dereference the data from $config. The following script shows examples of accessing the config scalar, array and hash data.

use YAML::XS qw/LoadFile/;
use feature qw/say/;

my $config = LoadFile('config.yaml');

# access the scalar emailName
my $emailName = $config->{emailName};

# access the array emailAddresses directly
my $firstEmailAddress = $config->{emailAddresses}->[0];
my $secondEmailAddress= $config->{emailAddresses}->[1];

# loop through and print emailAddresses
for (@{$config->{emailAddresses}}) { say }

# access the credentials hash key values directly
my $username = $config->{credentials}->{username};
my $password = $config->{credentials}->{password};

# loop through and print credentials
for (keys %{$config->{credentials}}) {
    say "$_: $config->{credentials}->{$_}";
}

More YAML

YAML::XS uses the libyaml C library and provides strong performance and adherence to the YAML specification. However if you have difficulty installing YAML::XS, there are pure Perl alternatives available: YAML is not actively maintained and has several bugs but excellent documentaion and generally works, YAML::Tiny is a newer module that implements a subset of the YAML specification.


David is the founder and editor of PerlTricks.com. A regular attendee of NY.pm, he works as a technology consultant in New York City.