Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple example of using data from a YAML configuration file in a Perl script

Tags:

yaml

perl

I need to create a YAML file to store some configuration data for a Perl script. This seems like it should be really easy but I haven't been able to work it out, I think if I had just one simple example to copy I'd be fine. I want to do something like this:

-----test.yaml-----
image_width: 500
show_values: 0
-------------------

------test.perl------
use YAML;

my (%settings) = Load('test.yaml');
print "The image width is", $settings{image_width};
---------------------
like image 599
user416625 Avatar asked Aug 10 '10 21:08

user416625


People also ask

How do I read a YAML file in Perl?

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 'LoadFile'; use Data::Dumper; my $config = LoadFile('config.

How are YAML files used?

What is YAML used for? One of the most common uses for YAML is to create configuration files. It's recommended that configuration files be written in YAML rather than JSON, even though they can be used interchangeably in most cases, because YAML has better readability and is more user-friendly.

What are YAML configuration files?

YAML is a digestible data serialization language often used to create configuration files with any programming language. Designed for human interaction, YAML is a strict superset of JSON, another data serialization language. But because it's a strict superset, it can do everything that JSON can and more.

How do I read a YAML config file in Python?

Reading a key from YAML config file We can read the data using yaml. load() method which takes file pointer and Loader as parameters. FullLoader handles the conversion from YAML scalar values to the Python dictionary. The index [0] is used to select the tag we want to read.


3 Answers

Try this:

#!/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};

– Michael

like image 113
mscha Avatar answered Oct 03 '22 21:10

mscha


Your fundamental problem here is that Load expects a string containing YAML, not a filename. You wanted LoadFile instead (which is not exported by default). Also, you should use YAML::XS instead of YAML if you can, as it's a better implementation. (But YAML should be adequate for a simple config file.)

The other problem is that LoadFile will return a hash reference (well, if your YAML looks like a hash, as yours does), not a list you can use to initialize a hash.

Try this:

use strict;
use warnings;
use YAML::XS qw(LoadFile);

my $settings = LoadFile('test.yaml');

print "The image width is ", $settings->{image_width};

(You can delete the ::XS if you don't want to (or can't) install YAML::XS. The program will work with no other changes.)

like image 28
cjm Avatar answered Sep 30 '22 21:09

cjm


Try dumping out the configuration you want.

use strict;
use warnings;

use YAML;

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

print Dump(\%settings);

This prints

---
bar:
  - one
  - two
  - three
foo: 1

Also, wikipedia has a good overview of YAML if the specification is a bit too dense.

like image 45
szbalint Avatar answered Oct 03 '22 21:10

szbalint