How to read multi-document YAML file in Perl?
Question
How to read multi-document YAML file in Perl?
Reading multi-document YAML content in Perl usually involves using a YAML parser like YAML::XS. However, in some environments—especially restricted sandboxes or limited Perl installations—YAML::XS might not be available or fully functional. If you encounter the error Undefined subroutine &YAML::XS::LoadAll, it means either the module is missing or that function isn't accessible.
Since external modules like YAML::XS are not core Perl modules and your sandbox forbids installing non-core modules, you cannot rely on them for YAML parsing in that environment. Unfortunately, Perl's core distribution does not include a YAML parser.
One common workaround in a constrained context is to parse multi-document YAML manually if you control the format and input — by splitting on the --- YAML document separator and handling each document as a simpler format or structured text, or by using a minimalist parser implemented directly in Perl. However, writing a robust YAML parser from scratch is complex due to YAML’s specification.
Example: Basic Manual Split of Multi-document YAML Text
This minimal example simulates reading a multi-document YAML-style string by splitting on the separator and printing each "document" as raw text. This does not parse YAML fully but demonstrates how to process multiple YAML documents without any external modules:
use strict;
use warnings;
# Example multi-document YAML string
my $yaml = <<'YAML';
---
name: Alice
age: 30
---
name: Bob
age: 25
hobbies:
- reading
- hiking
---
- apple
- banana
- cherry
YAML
# Split the string on YAML document delimiters (---)
my @documents = split /^---\s*$/m, $yaml;
# Remove any empty leading element if present
@documents = grep { /\S/ } @documents;
for my $index (0 .. $#documents) {
print "Document $index raw content:\n";
print $documents[$index];
print "\n\n";
}
Key Points and Considerations
YAML::XSis not a core module and requires installation; if you cannot install CPAN modules, you cannot use it.- Splitting by
---works well to identify YAML documents because YAML explicitly uses this separator. - This example does not parse YAML, so it cannot turn YAML into Perl data structures — it just separates document texts.
- To actually parse YAML safely and robustly, you need a dedicated YAML parser like YAML::XS, YAML::Tiny (smaller), or YAML::PP (pure Perl).
- Be cautious when manually processing YAML, especially when document content is nested or complex.
Summary
If you have YAML::XS available, you can use YAML::XS::LoadAll() fully qualified as shown in many resources. But in a sandbox environment without non-core modules, the best you can do is split the YAML stream on document boundaries and process each piece manually or with a custom parser.
For full YAML parsing, installing a CPAN YAML module in an unrestricted environment is strongly recommended.
Verified Code
Executed in a sandbox to capture real output. • v5.34.1 • 9ms
Document 0 raw content:
name: Alice
age: 30
Document 1 raw content:
name: Bob
age: 25
hobbies:
- reading
- hiking
Document 2 raw content:
- apple
- banana
- cherry
(empty)