NAME
TOON - Token-Oriented Object Notation for Perl
SYNOPSIS
use TOON qw(encode_toon decode_toon);
my $text = encode_toon({ answer => 42, active => 1 });
my $data = decode_toon($text);
DESCRIPTION
This is a small pure-Perl starter implementation of a TOON encoder/decoder with an interface inspired by JSON.
This version supports a pragmatic TOON syntax:
scalars: null, true, false, numbers, quoted strings
arrays: [ ... ]
objects: { key: value }
bareword object keys consisting of
[A-Za-z_][A-Za-z0-9_\-]*
Quoted strings use JSON-style escapes.
METHODS
new
my $toon = TOON->new(%opts);
Creates and returns a new TOON 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
prettyis enabled. Defaults to2.
encode
my $text = $toon->encode($data);
Encodes the given Perl data structure into a TOON string and returns it.
decode
my $data = $toon->decode($text);
Parses the given TOON string and returns the corresponding Perl data structure. Throws a TOON::Error exception if the input is invalid.
pretty
$toon->pretty; # enable pretty printing
$toon->pretty(1); # enable pretty printing
$toon->pretty(0); # disable pretty printing
Enables or disables pretty-printed output. When called without an argument (or with a true argument), pretty printing is enabled. Returns the TOON object so that calls can be chained.
canonical
$toon->canonical; # enable canonical output
$toon->canonical(1); # enable canonical output
$toon->canonical(0); # disable canonical output
Enables or disables canonical (sorted-key) output. Returns the TOON object so that calls can be chained.
indent
$toon->indent(4);
Sets the number of spaces used per indentation level when pretty printing is enabled. Returns the TOON object so that calls can be chained.
FUNCTIONS
encode_toon
use TOON qw(encode_toon);
my $text = encode_toon($data, %opts);
Functional interface to encoding. Encodes the given Perl data structure into a TOON string and returns it. Accepts the same options as "new".
decode_toon
use TOON qw(decode_toon);
my $data = decode_toon($text, %opts);
Functional interface to decoding. Parses the given TOON string and returns the corresponding Perl data structure. Throws a TOON::Error exception if the input is invalid. Accepts the same options as "new".
to_toon
use TOON qw(to_toon);
my $text = to_toon($data, %opts);
An alias for "encode_toon".
from_toon
use TOON qw(from_toon);
my $data = from_toon($text, %opts);
An alias for "decode_toon".
AUTHOR
Dave Cross <dave@perlhacks.com>