How to parse YAML file in Perl using YAML::XS?

Question

How to parse YAML file in Perl using YAML::XS?

Parsing YAML Data in Perl Using YAML::XS Without File Access

The YAML::XS module provides fast and reliable YAML parsing in Perl, but it requires reading from files with LoadFile. When running in a sandbox environment without filesystem access, reading from an actual YAML file is not possible.

Instead, you can parse YAML directly from a string using YAML::XS::Load. This function takes a YAML document as a string and returns the equivalent Perl data structure. This approach perfectly suits sandboxed or restricted environments where file access is disabled.

Key Concepts

  • Load($yaml_string) parses a YAML document string instead of a file.
  • Data structures returned are references (typically a hashref or arrayref).
  • Dereference using $scalar, @array, or %hash sigils accordingly.
  • eval blocks handle parsing errors gracefully.
  • This works well on Perl 5.8+ and with YAML::XS installed.

Common Pitfalls

  • Attempting to open or read files when the environment disallows it.
  • Not handling exceptions from invalid YAML input.
  • Dereferencing the returned data incorrectly (e.g., forgetting to use ->).

Runnable Example (No Files, Pure String Parsing)

Below is a complete Perl script parsing YAML from a string and printing the parsed content back to STDOUT. This runs with perl - in any environment without needing file system access.

#!/usr/bin/perl
use strict;
use warnings;
use YAML::XS qw(Load Dump);

# Sample YAML content as a multiline string
my $yaml_string = <<'END_YAML';
name: John Doe
age: 30
languages:
  - Perl
  - Python
  - YAML
END_YAML

my $data;
eval {
    $data = Load($yaml_string);
    1;
} or do {
    die "Failed to parse YAML string: $@";
};

print "Parsed YAML content:\n";
print Dump($data);

print "Name: $data->{name}\n";
print "Age: $data->{age}\n";

print "Known languages:\n";
foreach my $lang (@{$data->{languages}}) {
    print "- $lang\n";
}

Explanation

The example demonstrates parsing YAML from a literal string stored in $yaml_string. Using Load avoids any file I/O. The returned Perl structure $data is a hashref representing the YAML document.

The script then prints the entire data structure as YAML again with Dump to verify parsing. Finally, some elements are accessed by dereferencing the hashref and arrayref. The eval block ensures any parsing errors are caught, avoiding runtime exceptions.

Summary

When file access is restricted or unavailable, parse YAML data via strings using YAML::XS::Load. This aligns well with Perl's "There's More Than One Way To Do It" (TMTOWTDI) philosophy and lets you safely and cleanly handle YAML in sandboxed or limited environments.

Verified Code

Executed in a sandbox to capture real output. • v5.34.1 • 13ms

Tip: edit code and use “Run (Browser)”. Server runs always execute the published, verified snippet.
STDOUT
Parsed YAML content:
---
age: 30
languages:
- Perl
- Python
- YAML
name: John Doe
Name: John Doe
Age: 30
Known languages:
- Perl
- Python
- YAML
STDERR
(empty)

Was this helpful?

Related Questions