I have a yaml file which is generated from another source as shown below.
connect1:
connect2:
- { level1 : name, level2: age,
level3: gender}
My code looks something like this --?
use YAML::Tiny qw(LoadFile);
use YAML;
use YAML::Loader;
use YAML::Syck;
use YAML qw(LoadFile);
use Data::Dumper;
my $data = LoadFile("file.yaml");
my @config = $data->{connect1}->{connect2};
print Dumper(@config);
I'm getting this following error --> YAML Error: Invalid element in map Code: YAML_LOAD_ERR_BAD_MAP_ELEMENT Line: 3 Document: 1
If level1 , level2 and leve3 are in the same line then I dont see this issue. I see the issue because of indentation I think. But there is no way that I can change this file.yaml. So is there a way in perl that I can still parse this yaml file without modifying the file.yaml?
The state of YAML in Perl is a bit unfortunate, because there are several modules which support different features.
YAML::Syck, YAML::XS and YAML::PP can parse your example. YAML::XS is probably a good choice right now.
YAML.pm was the first perl module for YAML, and it was written for YAML 1.0.
YAML::Syck is based on libsyck, which was written for YAML 1.0. It can parse more than YAML.pm though.
YAML::XS is based on libyaml, written for YAML 1.1. You should be able to parse most YAML with it, and libyaml is used in (or was ported to) many other languages.
YAML::Tiny just supports a subset of YAML, which does not include flow collections { ... }, [ ... ] and aliases/anchors (&x, *x)
YAML::PP is pretty new and already can parse a lot, but it's also not complete yet. It aims to parse YAML 1.2 (and it will also partially support 1.1 in the future) Disclaimer: I'm the author
Here you can find my slides from the London Perl Workshop 2017: https://perlpunk.github.io/slides.lpw2017/yaml-where-and-how-to-use/
Starting at slide 24 you'll find a quick overview over the 5 modules.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With