PaulM
PaulM

Reputation: 375

How do I declare a list in YAML and read using PERL YAML::XS

I can create a value in YAML as such:

MYVAL: 1

I can load this in my PERL as follows:

my $settings = YAML::XS::LoadFile...
my $number_mine = $settings->{'MYVAL'};

I would want to create now an array of strings in YAML. I tried using - and --- but not seeing it

YAML?

MYARRAY: str1,str2,str3

PERL:

my @array_mine = $settings->{'MYARRAY'};

Upvotes: 1

Views: 463

Answers (2)

Anthon
Anthon

Reputation: 76692

This:

MYARRAY: str1,str2,str3

is a YAML mapping, the same way as your

MYVAL: 1

is a YAML mapping. The difference is that the value for the key MYARRAY is a plain (i.e. non quoted) scalar string str1,str2,str3 and for the value MYVAL is the scalar integer 1

If you want a sequence of three strings as value on a single line, you would need to do:

MYARRAY: [str1,str2,str3]

(optionally with whitespace before and/or after the commas). That is a flow style sequence of three plain scalars: str1, str2 and str3.

An alternative is to use block style:

MYARRAY: 
- str1
- str2
- str3

which is semantically equivalent to the flow style example above.

Upvotes: 3

mob
mob

Reputation: 118645

Dump out a list and see what it looks like:

$ perl -MYAML -E 'say YAML::Dump( { MYARRAY => ["str1","str2","str3"] })'
---
MYARRAY:
  - str1
  - str2
  - str3

Upvotes: 2

Related Questions