NAME

TOON::PP - Pure-Perl encoder/decoder for Token-Oriented Object Notation

SYNOPSIS

use TOON::PP;

my $pp   = TOON::PP->new(pretty => 1, canonical => 1);
my $text = $pp->encode({ answer => 42 });
my $data = $pp->decode($text);

DESCRIPTION

TOON::PP is the pure-Perl backend used by TOON. It implements a pragmatic TOON syntax that supports scalars (null, true, false, numbers, and quoted strings), arrays ([ ... ]), and objects ({ key: value }). Bareword object keys may consist of the characters [A-Za-z_][A-Za-z0-9_\-]*; all other keys must be quoted. Quoted strings use JSON-style escape sequences.

In most cases you will want to use the TOON front-end module rather than instantiating TOON::PP directly.

METHODS

new

my $pp = TOON::PP->new(%opts);

Creates and returns a new TOON::PP encoder/decoder object. Accepts the following optional named parameters:

pretty

Boolean. When true, output is formatted with newlines and indentation. Defaults to 0.

canonical

Boolean. When true, hash keys are sorted alphabetically in output. Defaults to 0.

indent

Integer. Number of spaces per indentation level when pretty is enabled. Defaults to 2.

encode

my $text = $pp->encode($data);

Encodes the given Perl data structure into a TOON string and returns it. Supported Perl types are: undef (encoded as null), plain scalars (encoded as numbers, booleans, or quoted strings), array references (encoded as TOON arrays), and hash references (encoded as TOON objects). Blessed references and unsupported reference types cause a TOON::Error exception to be thrown.

decode

my $data = $pp->decode($text);

Parses the given TOON string and returns the corresponding Perl data structure. Throws a TOON::Error exception if the input is not valid TOON.

AUTHOR

Dave Cross <dave@perlhacks.com>