I've been tasked with creating and editing quite a few YAML files, but rather than using an online syntax validator, I'm looking to run it through a script. Are there any available scripts that will test YAML Syntax? I found one through Perl that used perl::xs, but it wasn't particularly portable.
-
3There are several modules available for parsing YAML. They should all be able to give you some sort of feedback if your input contains syntactic errors.– simbabqueCommented Jan 30, 2013 at 19:14
-
Could you clarify "portable"? YAML::XS will work on just about any operating system. Do you mean something which can be shipped from machine to machine without recompiling?– SchwernCommented Mar 9, 2013 at 5:22
Add a comment
|
1 Answer
You should try YAML on CPAN, which is pure perl without dependencies, so it's portable. You can even just download it and put them with your validator script without installation.
Here is a example validator script:
use YAML qw(LoadFile);
use Data::Dumper;
my $data = LoadFile($ARGV[0]);
print Dumper($data);
If any error occurred, the error message will tell you where's the problem:
YAML Error: Expected separator '---'
Code: YAML_PARSE_ERR_NO_SEPARATOR
Line: 1
Document: 2
at /Users/alec/perl5/perlbrew/perls/p5161t/lib/site_perl/5.16.1/YAML/Loader.pm line 81.
-
1YAML.pm is a poor YAML implementation by its own admission. You can use YAML::Any to pick the best available YAML implementation installed.– SchwernCommented Mar 9, 2013 at 5:21
-
Agree. In fact I use YAML::XS in most of my projects. But since @securitygate mentioned his dislike of XS, I guess he wants a script level solution.– AlecCommented Mar 9, 2013 at 5:55
-
YAML::Any is a script level solution. It will pick the best installed version for you. If YAML::XS is available, it will use that. If its not, it'll use YAML.pm.– SchwernCommented Mar 9, 2013 at 21:54