2 # ABSTRACT: a minimal, pure perl TOML parser and serializer
6 no warnings qw(experimental);
9 use TOML::Tiny::Parser;
10 use TOML::Tiny::Writer;
12 use parent 'Exporter';
19 #-------------------------------------------------------------------------------
20 # TOML module compatibility
21 #-------------------------------------------------------------------------------
24 my $parser = TOML::Tiny::Parser->new(@_);
25 my $toml = eval{ $parser->parse($source) };
35 goto \&TOML::Tiny::Writer::to_toml;
38 #-------------------------------------------------------------------------------
40 #-------------------------------------------------------------------------------
42 my ($class, %param) = @_;
43 bless{ %param, parser => TOML::Tiny::Parser->new(%param) }, $class;
47 my ($self, $source) = @_;
48 $self->{parser}->parse;
52 my ($self, $data) = @_;
53 TOML::Tiny::Writer::to_toml($data,
54 strict_arrays => $self->{strict_arrays},
58 #-------------------------------------------------------------------------------
59 # For compatibility with TOML::from_toml's use of $TOML::Parser
60 #-------------------------------------------------------------------------------
69 use TOML::Tiny qw(from_toml to_toml);
71 binmode STDIN, ':encoding(UTF-8)';
72 binmode STDOUT, ':encoding(UTF-8)';
75 my $toml = do{ local $/; <STDIN> };
76 my ($parsed, $error) = from_toml $toml;
81 about => ['other', 'stuff'],
86 my $parser = TOML::Tiny->new;
87 my $data = $parser->decode($toml);
88 say $parser->encode($data);
93 C<TOML::Tiny> implements a pure-perl parser and generator for the
94 L<TOML|https://github.com/toml-lang/toml> data format. It conforms to TOML v5
95 (with a few caveats; see L</strict_arrays>) with support for more recent
96 changes in pursuit of v6.
98 C<TOML::Tiny> strives to maintain an interface compatible to the L<TOML> and
99 L<TOML::Parser> modules, and could even be used to override C<$TOML::Parser>:
104 local $TOML::Parser = TOML::Tiny->new(...);
110 C<TOML::Tiny> exports the following to functions for compatibility with the
111 L<TOML> module. See L<TOML/FUNCTIONS>.
115 Parses a string of C<TOML>-formatted source and returns the resulting data
116 structure. Any arguments after the first are passed to L<TOML::Tiny::Parser>'s
119 If there is a syntax error in the C<TOML> source, C<from_toml> will die with
120 an explanation which includes the line number of the error.
122 my $result = eval{ from_toml($toml_string) };
124 Alternately, this routine may be called in list context, in which case syntax
125 errors will result in returning two values, C<undef> and an error message.
127 my ($result, $error) = from_toml($toml_string);
129 Homogenous array strictures are enabled by passing C<strict_arrays>:
132 my $result = from_toml(q{mixed=[1, 2, "three"]})
134 Additional arguments may be passed after the toml source string; see L</new>.
138 Encodes a hash ref as a C<TOML>-formatted string.
140 my $toml = to_toml({foo => {'bar' => 'bat'}});
145 Homogenous array strictures are enabled by passing C<strict_arrays>:
148 my $toml = to_toml({mixed => [1, 2, "three"]}, strict_arrays => 1);
156 =item inflate_datetime
158 By default, C<TOML::Tiny> treats TOML datetimes as strings in the generated
159 data structure. The C<inflate_datetime> parameter allows the caller to provide
160 a routine to intercept those as they are generated:
162 use DateTime::Format::RFC3339;
164 my $parser = TOML::Tiny->new(
165 inflate_datetime => sub{
166 my $dt_string = shift;
167 return DateTime::Format::RFC3339->parse_datetime($dt_string);
171 =item inflate_boolean
173 By default, boolean values in a C<TOML> document result in a C<1> or C<0>.
174 If L<Types::Serialiser> is installed, they will instead be C<Types::Serialiser::true>
175 or C<Types::Serialiser::false>.
177 If you wish to override this, you can provide your own routine to generate values:
179 my $parser = TOML::Tiny->new(
180 inflate_boolean => sub{
182 if ($bool eq 'true') {
192 C<TOML v5> specified homogenous arrays. This has since been removed and will no
193 longer be part of the standard as of C<v6> (as of the time of writing; the
194 author of C<TOML> has gone back and forth on the issue, so no guarantees).
196 By default, C<TOML::Tiny> is flexible and supports heterogenous arrays. If you
197 wish to require strictly typed arrays (for C<TOML>'s definition of "type",
198 anyway), C<strict_arrays> will produce an error when encountering arrays with
205 Decodes C<TOML> and returns a hash ref. Dies on parse error.
209 Encodes a perl hash ref as a C<TOML>-formatted string. Dies when encountering
210 an array of mixed types if C<strict_arrays> was set.
214 Alias for C<encode> to provide compatibility with C<TOML::Parser> when
215 overriding the parser by setting C<$TOML::Parser>.
218 =head1 DIFFERENCES FROM L<TOML> AND L<TOML::Parser>
220 C<TOML::Tiny> differs in a few significant ways from the L<TOML> module,
221 particularly in adding support for newer C<TOML> features and strictness.
223 L<TOML> defaults to lax parsing and provides C<strict_mode> to (slightly)
224 tighten things up. C<TOML::Tiny> defaults to (somehwat) stricter parsing, with
225 the exception of permitting heterogenous arrays (illegal in v4 and v5, but
226 permissible in the upcoming v6); optional enforcement of homogenous arrays is
227 supported with C<strict_arrays>.
229 C<TOML::Tiny> ignores invalid surrogate pairs within basic and multiline
230 strings (L<TOML> may attempt to decode an invalid pair). Additionally, only
231 those character escapes officially supported by TOML are interpreted as such by
234 C<TOML::Tiny> supports stripping initial whitespace and handles lines
235 terminating with a backslash correctly in multilne strings:
248 {x => 'foo', y => 'how now brown bureaucrat.'}
250 C<TOML::Tiny> includes support for integers specified in binary, octal or hex
251 as well as the special float values C<inf> and C<nan>.
253 =head1 ACKNOWLEDGEMENTS
255 Thanks to L<ZipRecruiter|https://www.ziprecruiter.com> for encouraging their
256 employees to contribute back to the open source ecosystem. Without their
257 dedication to quality software development this distribution would not exist.