# perldoc > YAML

---
type: CommandReference
command: YAML
mode: perldoc
section: ''
source: perldoc
---

## Quick Reference

- `use YAML;` — Import default functions `Dump` and `Load`
- `use YAML ();` — Import nothing, use fully qualified names
- `Dump($data1, $data2, ...)` — Serialize Perl data structures to YAML string
- `Load($yaml_string)` — Deserialize YAML string to list of Perl data structures
- `DumpFile($file, $list)` — Write YAML to a file
- `LoadFile($file)` — Read YAML from a file
- `$YAML::Indent = 3;` — Change indentation width (default 2)
- `$YAML::SortKeys = 0;` — Disable hash key sorting on dump

## Name

YAML.pm — YAML Ain't Markup Language (YAML 1.0 serializer/deserializer for Perl)

## Synopsis

perl
use YAML;

# Load a YAML stream of 3 documents into Perl data structures
my ($hashref, $arrayref, $string) = Load(<<'...');
---
name: ingy
age: old
weight: heavy
favorite colors:
  - red
  - green
  - blue
---
- Clark Evans
- Oren Ben-Kiki
- Ingy döt Net
--- >
You probably think YAML stands for "Yet Another Markup Language".
It ain't! YAML is really a data serialization language.
...

# Dump Perl data structures back into YAML
print Dump($string, $arrayref, $hashref);

# DumpFile/LoadFile for file I/O
DumpFile('/path/to/file.yaml', $data);
my $data = LoadFile('/path/to/file.yaml');
## Options

Global options are set as variables in the `YAML` namespace (e.g., `$YAML::Indent`).

- `$YAML::DumperClass` — Override the class used for dumping
- `$YAML::LoadBlessed` (since 1.25, default false) — When true, YAML nodes with special tags are automatically blessed into objects. Disable when loading untrusted YAML.
- `$YAML::LoaderClass` — Override the class used for loading
- `$YAML::Indent` (default 2) — Number of spaces per indentation level
- `$YAML::SortKeys` (default 1) — Sort hash keys on dump. Set to 2 to override `YAML::Node` order.
- `$YAML::Stringify` (default 0) — Honor string overloading when dumping objects
- `$YAML::Numify` (default 0) — Autodetect numbers and numify them on load
- `$YAML::UseHeader` (default 1) — Include the YAML header separator (`---`) on the first document
- `$YAML::UseVersion` (default 0) — Include `%YAML:1.0` in the header
- `$YAML::AnchorPrefix` (default '') — String prepended to numeric anchor names
- `$YAML::UseCode` — Shortcut to set both `DumpCode` and `LoadCode`
- `$YAML::DumpCode` (default placeholder) — Set to `1` or `'deparse'` to serialize code references as Perl code via `B::Deparse`
- `$YAML::LoadCode` — Set to `1` or `'deparse'` to allow `eval()` of code references on load. Dangerous.
- `$YAML::Preserve` (default 0) — Load hashes as `YAML::Node` tied objects to preserve key order
- `$YAML::UseBlock` (default 0) — Force block style for all multiline scalars
- `$YAML::UseAliases` (default 1) — Enable alias markers for duplicate references. Set to 0 to disable (dangerous for recursive data).
- `$YAML::CompressSeries` (default 1) — Compress arrays of hashes into compact form
- `$YAML::QuoteNumericStrings` (default 0) — Quote strings that look like numbers to preserve formatting

## Examples

**Dump with custom key order (using Bless)**

perl
use YAML qw(Dump Bless);
$hash = {apple => 'good', banana => 'bad', cauliflower => 'ugly'};
print Dump $hash;
Bless($hash)->keys(['banana', 'apple']);
print Dump $hash;
Output:

---
apple: good
banana: bad
cauliflower: ugly
---
banana: bad
apple: good
**Use YAML as a Storable replacement**

perl
use YAML qw(freeze thaw);
my $serialized = freeze($data);
my $data = thaw($serialized);
## See Also

- [YAML::XS](http://localhost/phpMan.php/perldoc/YAML%3A%3AXS/markdown) — Recommended fast C-based YAML implementation
- [YAML::Tiny](http://localhost/phpMan.php/perldoc/YAML%3A%3ATiny/markdown) — Minimal pure-Perl YAML subset
- [YAML::Syck](http://localhost/phpMan.php/perldoc/YAML%3A%3ASyck/markdown) — Perl binding to libsyck
- [YAML::Node](http://localhost/phpMan.php/perldoc/YAML%3A%3ANode/markdown) — Tied hash for preserving key order
- [YAML::Old](http://localhost/phpMan.php/perldoc/YAML%3A%3AOld/markdown) — Legacy version of this module
- [Data::Dumper](http://localhost/phpMan.php/perldoc/Data%3A%3ADumper/markdown) — Alternative Perl serialization
- [YAML specification](http://www.yaml.org/spec/) — YAML 1.2 spec