<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package YAML::Dumper;
use YAML::Mo;
extends 'YAML::Dumper::Base';

our $VERSION = '0.84';

use YAML::Dumper::Base;
use YAML::Node;
use YAML::Types;

# Context constants
use constant KEY       =&gt; 3;
use constant BLESSED   =&gt; 4;
use constant FROMARRAY =&gt; 5;
use constant VALUE     =&gt; "\x07YAML\x07VALUE\x07";

# Common YAML character sets
my $ESCAPE_CHAR = '[\\x00-\\x08\\x0b-\\x0d\\x0e-\\x1f]';
my $LIT_CHAR    = '|';    

#==============================================================================
# OO version of Dump. YAML-&gt;new-&gt;dump($foo); 
sub dump {
    my $self = shift;
    $self-&gt;stream('');
    $self-&gt;document(0);
    for my $document (@_) {
        $self-&gt;{document}++;
        $self-&gt;transferred({});
        $self-&gt;id_refcnt({});
        $self-&gt;id_anchor({});
        $self-&gt;anchor(1);
        $self-&gt;level(0);
        $self-&gt;offset-&gt;[0] = 0 - $self-&gt;indent_width;
        $self-&gt;_prewalk($document);
        $self-&gt;_emit_header($document);
        $self-&gt;_emit_node($document);
    }
    return $self-&gt;stream;
}

# Every YAML document in the stream must begin with a YAML header, unless
# there is only a single document and the user requests "no header".
sub _emit_header {
    my $self = shift;
    my ($node) = @_;
    if (not $self-&gt;use_header and 
        $self-&gt;document == 1
       ) {
        $self-&gt;die('YAML_DUMP_ERR_NO_HEADER')
          unless ref($node) =~ /^(HASH|ARRAY)$/;
        $self-&gt;die('YAML_DUMP_ERR_NO_HEADER')
          if ref($node) eq 'HASH' and keys(%$node) == 0;
        $self-&gt;die('YAML_DUMP_ERR_NO_HEADER')
          if ref($node) eq 'ARRAY' and @$node == 0;
        # XXX Also croak if aliased, blessed, or ynode
        $self-&gt;headless(1);
        return;
    }
    $self-&gt;{stream} .= '---';
# XXX Consider switching to 1.1 style
    if ($self-&gt;use_version) {
#         $self-&gt;{stream} .= " #YAML:1.0";
    }
}

# Walk the tree to be dumped and keep track of its reference counts.
# This function is where the Dumper does all its work. All type
# transfers happen here.
sub _prewalk {
    my $self = shift;
    my $stringify = $self-&gt;stringify;
    my ($class, $type, $node_id) = $self-&gt;node_info(\$_[0], $stringify);

    # Handle typeglobs
    if ($type eq 'GLOB') {
        $self-&gt;transferred-&gt;{$node_id} =
          YAML::Type::glob-&gt;yaml_dump($_[0]);
        $self-&gt;_prewalk($self-&gt;transferred-&gt;{$node_id});
        return;
    }

    # Handle regexps
    if (ref($_[0]) eq 'Regexp') {  
        return;
    }

    # Handle Purity for scalars.
    # XXX can't find a use case yet. Might be YAGNI.
    if (not ref $_[0]) {
        $self-&gt;{id_refcnt}{$node_id}++ if $self-&gt;purity;
        return;
    }

    # Make a copy of original
    my $value = $_[0];
    ($class, $type, $node_id) = $self-&gt;node_info($value, $stringify);

    # Must be a stringified object.
    return if (ref($value) and not $type);

    # Look for things already transferred.
    if ($self-&gt;transferred-&gt;{$node_id}) {
        (undef, undef, $node_id) = (ref $self-&gt;transferred-&gt;{$node_id})
          ? $self-&gt;node_info($self-&gt;transferred-&gt;{$node_id}, $stringify)
          : $self-&gt;node_info(\ $self-&gt;transferred-&gt;{$node_id}, $stringify);
        $self-&gt;{id_refcnt}{$node_id}++;
        return;
    }

    # Handle code refs
    if ($type eq 'CODE') {
        $self-&gt;transferred-&gt;{$node_id} = 'placeholder';
        YAML::Type::code-&gt;yaml_dump(
            $self-&gt;dump_code,
            $_[0], 
            $self-&gt;transferred-&gt;{$node_id}
        );
        ($class, $type, $node_id) = 
          $self-&gt;node_info(\ $self-&gt;transferred-&gt;{$node_id}, $stringify);
        $self-&gt;{id_refcnt}{$node_id}++;
        return;
    }

    # Handle blessed things
    if (defined $class) {
        if ($value-&gt;can('yaml_dump')) {
            $value = $value-&gt;yaml_dump;
        }
        elsif ($type eq 'SCALAR') {
            $self-&gt;transferred-&gt;{$node_id} = 'placeholder';
            YAML::Type::blessed-&gt;yaml_dump
              ($_[0], $self-&gt;transferred-&gt;{$node_id});
            ($class, $type, $node_id) =
              $self-&gt;node_info(\ $self-&gt;transferred-&gt;{$node_id}, $stringify);
            $self-&gt;{id_refcnt}{$node_id}++;
            return;
        }
        else {
            $value = YAML::Type::blessed-&gt;yaml_dump($value);
        }
        $self-&gt;transferred-&gt;{$node_id} = $value;
        (undef, $type, $node_id) = $self-&gt;node_info($value, $stringify);
    }

    # Handle YAML Blessed things
    if (defined YAML-&gt;global_object()-&gt;{blessed_map}{$node_id}) {
        $value = YAML-&gt;global_object()-&gt;{blessed_map}{$node_id};
        $self-&gt;transferred-&gt;{$node_id} = $value;
        ($class, $type, $node_id) = $self-&gt;node_info($value, $stringify);
        $self-&gt;_prewalk($value);
        return;
    }

    # Handle hard refs
    if ($type eq 'REF' or $type eq 'SCALAR') {
        $value = YAML::Type::ref-&gt;yaml_dump($value);
        $self-&gt;transferred-&gt;{$node_id} = $value;
        (undef, $type, $node_id) = $self-&gt;node_info($value, $stringify);
    }

    # Handle ref-to-glob's
    elsif ($type eq 'GLOB') {
        my $ref_ynode = $self-&gt;transferred-&gt;{$node_id} =
          YAML::Type::ref-&gt;yaml_dump($value);

        my $glob_ynode = $ref_ynode-&gt;{&amp;VALUE} = 
          YAML::Type::glob-&gt;yaml_dump($$value);

        (undef, undef, $node_id) = $self-&gt;node_info($glob_ynode, $stringify);
        $self-&gt;transferred-&gt;{$node_id} = $glob_ynode;
        $self-&gt;_prewalk($glob_ynode);
        return;
    }

    # Increment ref count for node
    return if ++($self-&gt;{id_refcnt}{$node_id}) &gt; 1;

    # Keep on walking
    if ($type eq 'HASH') {
        $self-&gt;_prewalk($value-&gt;{$_})
            for keys %{$value};
        return;
    }
    elsif ($type eq 'ARRAY') {
        $self-&gt;_prewalk($_)
            for @{$value};
        return;
    }

    # Unknown type. Need to know about it.
    $self-&gt;warn(&lt;&lt;"...");
YAML::Dumper can't handle dumping this type of data.
Please report this to the author.

id:    $node_id
type:  $type
class: $class
value: $value

...

    return;
}

# Every data element and sub data element is a node.
# Everything emitted goes through this function.
sub _emit_node {
    my $self = shift;
    my ($type, $node_id);
    my $ref = ref($_[0]);
    if ($ref) {
        if ($ref eq 'Regexp') {
            $self-&gt;_emit(' !!perl/regexp');
            $self-&gt;_emit_str("$_[0]");
            return;
        }
        (undef, $type, $node_id) = $self-&gt;node_info($_[0], $self-&gt;stringify);
    }
    else {
        $type = $ref || 'SCALAR';
        (undef, undef, $node_id) = $self-&gt;node_info(\$_[0], $self-&gt;stringify);
    }

    my ($ynode, $tag) = ('') x 2;
    my ($value, $context) = (@_, 0);

    if (defined $self-&gt;transferred-&gt;{$node_id}) {
        $value = $self-&gt;transferred-&gt;{$node_id};
        $ynode = ynode($value);
        if (ref $value) {
            $tag = defined $ynode ? $ynode-&gt;tag-&gt;short : '';
            (undef, $type, $node_id) =
              $self-&gt;node_info($value, $self-&gt;stringify);
        }
        else {
            $ynode = ynode($self-&gt;transferred-&gt;{$node_id});
            $tag = defined $ynode ? $ynode-&gt;tag-&gt;short : '';
            $type = 'SCALAR';
            (undef, undef, $node_id) = 
              $self-&gt;node_info(
                  \ $self-&gt;transferred-&gt;{$node_id},
                  $self-&gt;stringify
              );
        }
    }
    elsif ($ynode = ynode($value)) {
        $tag = $ynode-&gt;tag-&gt;short;
    }

    if ($self-&gt;use_aliases) {
        $self-&gt;{id_refcnt}{$node_id} ||= 0;
        if ($self-&gt;{id_refcnt}{$node_id} &gt; 1) {
            if (defined $self-&gt;{id_anchor}{$node_id}) {
                $self-&gt;{stream} .= ' *' . $self-&gt;{id_anchor}{$node_id} . "\n";
                return;
            }
            my $anchor = $self-&gt;anchor_prefix . $self-&gt;{anchor}++;
            $self-&gt;{stream} .= ' &amp;' . $anchor;
            $self-&gt;{id_anchor}{$node_id} = $anchor;
        }
    }

    return $self-&gt;_emit_str("$value")   # Stringified object
      if ref($value) and not $type;
    return $self-&gt;_emit_scalar($value, $tag)
      if $type eq 'SCALAR' and $tag;
    return $self-&gt;_emit_str($value)
      if $type eq 'SCALAR';
    return $self-&gt;_emit_mapping($value, $tag, $node_id, $context)
      if $type eq 'HASH';
    return $self-&gt;_emit_sequence($value, $tag)
      if $type eq 'ARRAY';
    $self-&gt;warn('YAML_DUMP_WARN_BAD_NODE_TYPE', $type);
    return $self-&gt;_emit_str("$value");
}

# A YAML mapping is akin to a Perl hash. 
sub _emit_mapping {
    my $self = shift;
    my ($value, $tag, $node_id, $context) = @_;
    $self-&gt;{stream} .= " !$tag" if $tag;

    # Sometimes 'keys' fails. Like on a bad tie implementation.
    my $empty_hash = not(eval {keys %$value});
    $self-&gt;warn('YAML_EMIT_WARN_KEYS', $@) if $@;
    return ($self-&gt;{stream} .= " {}\n") if $empty_hash;

    # If CompressSeries is on (default) and legal is this context, then
    # use it and make the indent level be 2 for this node.
    if ($context == FROMARRAY and
        $self-&gt;compress_series and
        not (defined $self-&gt;{id_anchor}{$node_id} or $tag or $empty_hash)
       ) {
        $self-&gt;{stream} .= ' ';
        $self-&gt;offset-&gt;[$self-&gt;level+1] = $self-&gt;offset-&gt;[$self-&gt;level] + 2;
    }
    else {
        $context = 0;
        $self-&gt;{stream} .= "\n"
          unless $self-&gt;headless &amp;&amp; not($self-&gt;headless(0));
        $self-&gt;offset-&gt;[$self-&gt;level+1] =
          $self-&gt;offset-&gt;[$self-&gt;level] + $self-&gt;indent_width;
    }

    $self-&gt;{level}++;
    my @keys;
    if ($self-&gt;sort_keys == 1) {
        if (ynode($value)) {
            @keys = keys %$value;
        }
        else {
            @keys = sort keys %$value;
        }
    }
    elsif ($self-&gt;sort_keys == 2) {
        @keys = sort keys %$value;
    }
    # XXX This is hackish but sometimes handy. Not sure whether to leave it in.
    elsif (ref($self-&gt;sort_keys) eq 'ARRAY') {
        my $i = 1;
        my %order = map { ($_, $i++) } @{$self-&gt;sort_keys};
        @keys = sort {
            (defined $order{$a} and defined $order{$b})
              ? ($order{$a} &lt;=&gt; $order{$b})
              : ($a cmp $b);
        } keys %$value;
    }
    else {
        @keys = keys %$value;
    }
    # Force the YAML::VALUE ('=') key to sort last.
    if (exists $value-&gt;{&amp;VALUE}) {
        for (my $i = 0; $i &lt; @keys; $i++) {
            if ($keys[$i] eq &amp;VALUE) {
                splice(@keys, $i, 1);
                push @keys, &amp;VALUE;
                last;
            }
        }
    }

    for my $key (@keys) {
        $self-&gt;_emit_key($key, $context);
        $context = 0;
        $self-&gt;{stream} .= ':';
        $self-&gt;_emit_node($value-&gt;{$key});
    }
    $self-&gt;{level}--;
}

# A YAML series is akin to a Perl array.
sub _emit_sequence {
    my $self = shift;
    my ($value, $tag) = @_;
    $self-&gt;{stream} .= " !$tag" if $tag;

    return ($self-&gt;{stream} .= " []\n") if @$value == 0;
        
    $self-&gt;{stream} .= "\n"
      unless $self-&gt;headless &amp;&amp; not($self-&gt;headless(0));

    # XXX Really crufty feature. Better implemented by ynodes.
    if ($self-&gt;inline_series and
        @$value &lt;= $self-&gt;inline_series and
        not (scalar grep {ref or /\n/} @$value)
       ) {
        $self-&gt;{stream} =~ s/\n\Z/ /;
        $self-&gt;{stream} .= '[';
        for (my $i = 0; $i &lt; @$value; $i++) {
            $self-&gt;_emit_str($value-&gt;[$i], KEY);
            last if $i == $#{$value};
            $self-&gt;{stream} .= ', ';
        }
        $self-&gt;{stream} .= "]\n";
        return;
    }

    $self-&gt;offset-&gt;[$self-&gt;level + 1] =
      $self-&gt;offset-&gt;[$self-&gt;level] + $self-&gt;indent_width;
    $self-&gt;{level}++;
    for my $val (@$value) {
        $self-&gt;{stream} .= ' ' x $self-&gt;offset-&gt;[$self-&gt;level];
        $self-&gt;{stream} .= '-';
        $self-&gt;_emit_node($val, FROMARRAY);
    }
    $self-&gt;{level}--;
}

# Emit a mapping key
sub _emit_key {
    my $self = shift;
    my ($value, $context) = @_;
    $self-&gt;{stream} .= ' ' x $self-&gt;offset-&gt;[$self-&gt;level]
      unless $context == FROMARRAY;
    $self-&gt;_emit_str($value, KEY);
}

# Emit a blessed SCALAR
sub _emit_scalar {
    my $self = shift;
    my ($value, $tag) = @_;
    $self-&gt;{stream} .= " !$tag";
    $self-&gt;_emit_str($value, BLESSED);
}

sub _emit {
    my $self = shift;
    $self-&gt;{stream} .= join '', @_;
}

# Emit a string value. YAML has many scalar styles. This routine attempts to
# guess the best style for the text.
sub _emit_str {
    my $self = shift;
    my $type = $_[1] || 0;

    # Use heuristics to find the best scalar emission style.
    $self-&gt;offset-&gt;[$self-&gt;level + 1] =
      $self-&gt;offset-&gt;[$self-&gt;level] + $self-&gt;indent_width;
    $self-&gt;{level}++;

    my $sf = $type == KEY ? '' : ' ';
    my $sb = $type == KEY ? '? ' : ' ';
    my $ef = $type == KEY ? '' : "\n";
    my $eb = "\n";

    while (1) {
        $self-&gt;_emit($sf),
        $self-&gt;_emit_plain($_[0]),
        $self-&gt;_emit($ef), last 
          if not defined $_[0];
        $self-&gt;_emit($sf, '=', $ef), last
          if $_[0] eq VALUE;
        $self-&gt;_emit($sf),
        $self-&gt;_emit_double($_[0]),
        $self-&gt;_emit($ef), last
          if $_[0] =~ /$ESCAPE_CHAR/;
        if ($_[0] =~ /\n/) {
            $self-&gt;_emit($sb),
            $self-&gt;_emit_block($LIT_CHAR, $_[0]),
            $self-&gt;_emit($eb), last
              if $self-&gt;use_block;
              Carp::cluck "[YAML] \$UseFold is no longer supported"
              if $self-&gt;use_fold;
            $self-&gt;_emit($sf),
            $self-&gt;_emit_double($_[0]),
            $self-&gt;_emit($ef), last
              if length $_[0] &lt;= 30;
            $self-&gt;_emit($sf),
            $self-&gt;_emit_double($_[0]),
            $self-&gt;_emit($ef), last
              if $_[0] !~ /\n\s*\S/;
            $self-&gt;_emit($sb),
            $self-&gt;_emit_block($LIT_CHAR, $_[0]),
            $self-&gt;_emit($eb), last;
        }
        $self-&gt;_emit($sf),
        $self-&gt;_emit_plain($_[0]),
        $self-&gt;_emit($ef), last
          if $self-&gt;is_valid_plain($_[0]);
        $self-&gt;_emit($sf),
        $self-&gt;_emit_double($_[0]),
        $self-&gt;_emit($ef), last
          if $_[0] =~ /'/;
        $self-&gt;_emit($sf),
        $self-&gt;_emit_single($_[0]),
        $self-&gt;_emit($ef);
        last;
    }

    $self-&gt;{level}--;

    return;
}

# Check whether or not a scalar should be emitted as an plain scalar.
sub is_valid_plain {
    my $self = shift;
    return 0 unless length $_[0];
    # refer to YAML::Loader::parse_inline_simple()
    return 0 if $_[0] =~ /^[\s\{\[\~\`\'\"\!\@\#\&gt;\|\%\&amp;\?\*\^]/;
    return 0 if $_[0] =~ /[\{\[\]\},]/;
    return 0 if $_[0] =~ /[:\-\?]\s/;
    return 0 if $_[0] =~ /\s#/;
    return 0 if $_[0] =~ /\:(\s|$)/;
    return 0 if $_[0] =~ /[\s\|\&gt;]$/;
    return 1;
}

sub _emit_block {
    my $self = shift;
    my ($indicator, $value) = @_;
    $self-&gt;{stream} .= $indicator;
    $value =~ /(\n*)\Z/;
    my $chomp = length $1 ? (length $1 &gt; 1) ? '+' : '' : '-';
    $value = '~' if not defined $value;
    $self-&gt;{stream} .= $chomp;
    $self-&gt;{stream} .= $self-&gt;indent_width if $value =~ /^\s/;
    $self-&gt;{stream} .= $self-&gt;indent($value);
}

# Plain means that the scalar is unquoted.
sub _emit_plain {
    my $self = shift;
    $self-&gt;{stream} .= defined $_[0] ? $_[0] : '~';
}

# Double quoting is for single lined escaped strings.
sub _emit_double {
    my $self = shift;
    (my $escaped = $self-&gt;escape($_[0])) =~ s/"/\\"/g;
    $self-&gt;{stream} .= qq{"$escaped"};
}

# Single quoting is for single lined unescaped strings.
sub _emit_single {
    my $self = shift;
    my $item = shift;
    $item =~ s{'}{''}g;
    $self-&gt;{stream} .= "'$item'";
}

#==============================================================================
# Utility subroutines.
#==============================================================================

# Indent a scalar to the current indentation level.
sub indent {
    my $self = shift;
    my ($text) = @_;
    return $text unless length $text;
    $text =~ s/\n\Z//;
    my $indent = ' ' x $self-&gt;offset-&gt;[$self-&gt;level];
    $text =~ s/^/$indent/gm;
    $text = "\n$text";
    return $text;
}

# Escapes for unprintable characters
my @escapes = qw(\0   \x01 \x02 \x03 \x04 \x05 \x06 \a
                 \x08 \t   \n   \v   \f   \r   \x0e \x0f
                 \x10 \x11 \x12 \x13 \x14 \x15 \x16 \x17
                 \x18 \x19 \x1a \e   \x1c \x1d \x1e \x1f
                );

# Escape the unprintable characters
sub escape {
    my $self = shift;
    my ($text) = @_;
    $text =~ s/\\/\\\\/g;
    $text =~ s/([\x00-\x1f])/$escapes[ord($1)]/ge;
    return $text;
}

1;

__END__

=head1 NAME

YAML::Dumper - YAML class for dumping Perl objects to YAML

=head1 SYNOPSIS

    use YAML::Dumper;
    my $dumper = YAML::Dumper-&gt;new;
    $dumper-&gt;indent_width(4);
    print $dumper-&gt;dump({foo =&gt; 'bar'});

=head1 DESCRIPTION

YAML::Dumper is the module that YAML.pm used to serialize Perl objects to
YAML. It is fully object oriented and usable on its own.

=head1 AUTHOR

Ingy dÃ¶t Net &lt;ingy@cpan.org&gt;

=head1 COPYRIGHT

Copyright (c) 2006, 2011-2012. Ingy dÃ¶t Net. All rights reserved.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

See L&lt;http://www.perl.com/perl/misc/Artistic.html&gt;

=cut
</pre></body></html>