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($source);
52 my ($self, $data) = @_;
53 TOML::Tiny::Writer::to_toml($data,
54 strict_arrays => $self->{strict_arrays},
55 no_string_guessing => $self->{no_string_guessing},
59 #-------------------------------------------------------------------------------
60 # For compatibility with TOML::from_toml's use of $TOML::Parser
61 #-------------------------------------------------------------------------------
70 use TOML::Tiny qw(from_toml to_toml);
72 binmode STDIN, ':encoding(UTF-8)';
73 binmode STDOUT, ':encoding(UTF-8)';
76 my $toml = do{ local $/; <STDIN> };
77 my ($parsed, $error) = from_toml $toml;
82 about => ['other', 'stuff'],
87 my $parser = TOML::Tiny->new;
88 my $data = $parser->decode($toml);
89 say $parser->encode($data);
94 C<TOML::Tiny> implements a pure-perl parser and generator for the
95 L<TOML|https://github.com/toml-lang/toml> data format. It conforms to TOML v5
96 (with a few caveats; see L</strict_arrays>) with support for more recent
97 changes in pursuit of v6.
99 C<TOML::Tiny> strives to maintain an interface compatible to the L<TOML> and
100 L<TOML::Parser> modules, and could even be used to override C<$TOML::Parser>:
105 local $TOML::Parser = TOML::Tiny->new(...);
111 C<TOML::Tiny> exports the following to functions for compatibility with the
112 L<TOML> module. See L<TOML/FUNCTIONS>.
116 Parses a string of C<TOML>-formatted source and returns the resulting data
117 structure. Any arguments after the first are passed to L<TOML::Tiny::Parser>'s
120 If there is a syntax error in the C<TOML> source, C<from_toml> will die with
121 an explanation which includes the line number of the error.
123 my $result = eval{ from_toml($toml_string) };
125 Alternately, this routine may be called in list context, in which case syntax
126 errors will result in returning two values, C<undef> and an error message.
128 my ($result, $error) = from_toml($toml_string);
130 Homogenous array strictures are enabled by passing C<strict_arrays>:
133 my $result = from_toml(q{mixed=[1, 2, "three"]})
135 Additional arguments may be passed after the toml source string; see L</new>.
139 Encodes a hash ref as a C<TOML>-formatted string.
141 my $toml = to_toml({foo => {'bar' => 'bat'}});
146 Homogenous array strictures are enabled by passing C<strict_arrays>:
149 my $toml = to_toml({mixed => [1, 2, "three"]}, strict_arrays => 1);
157 =item inflate_datetime
159 By default, C<TOML::Tiny> treats TOML datetimes as strings in the generated
160 data structure. The C<inflate_datetime> parameter allows the caller to provide
161 a routine to intercept those as they are generated:
163 use DateTime::Format::RFC3339;
165 my $parser = TOML::Tiny->new(
166 inflate_datetime => sub{
167 my $dt_string = shift;
168 return DateTime::Format::RFC3339->parse_datetime($dt_string);
172 =item inflate_boolean
174 By default, boolean values in a C<TOML> document result in a C<1> or C<0>.
175 If L<Types::Serialiser> is installed, they will instead be C<Types::Serialiser::true>
176 or C<Types::Serialiser::false>.
178 If you wish to override this, you can provide your own routine to generate values:
180 my $parser = TOML::Tiny->new(
181 inflate_boolean => sub{
183 if ($bool eq 'true') {
191 =item inflate_integer
193 TOML integers are 64 bit and may not match the size of the compiled perl's
194 internal integer type. By default, integers other than smallish
195 decimal integers are left as-is as perl strings which may be upgraded
196 as needed by the caller.
198 my $parser = TOML::Tiny->new(
199 inflate_integer => sub{
207 TOML floats are 64 bit and may not match the size of the compiled perl's
208 internal float type. By default, integers are left as-is as perl strings which
209 may be upgraded as needed by the caller.
211 my $parser = TOML::Tiny->new(
212 inflate_float => sub{
220 C<TOML v5> specified homogenous arrays. This has since been removed and will no
221 longer be part of the standard as of C<v6> (as of the time of writing; the
222 author of C<TOML> has gone back and forth on the issue, so no guarantees).
224 By default, C<TOML::Tiny> is flexible and supports heterogenous arrays. If you
225 wish to require strictly typed arrays (for C<TOML>'s definition of "type",
226 anyway), C<strict_arrays> will produce an error when encountering arrays with
229 =item no_string_guessing
231 When encoding a Perl scalar it is not always clear what the TOML type
232 of the value is supposed to be. By default, C<TOML::Tiny> will, for
233 unblessed scalars, guess based on the scalar's appearance. Strings
234 that look like numbers, or like datetimes, will be encoded as such.
236 With no_string_guessing, C<TOML::Tiny> will look at the perl innards
237 to find the currently stored value type. If it is a number, the
238 scalar will be encoded as a number. If it's a string, as a string.
239 Dates and times which weren't built with DateTime come out as strings.
241 Specifying C<inflate_float>, C<inflate_integer>, and
242 C<inflate_datetime> is likely to be helpful with this option.
248 Decodes C<TOML> and returns a hash ref. Dies on parse error.
252 Encodes a perl hash ref as a C<TOML>-formatted string. Dies when encountering
253 an array of mixed types if C<strict_arrays> was set.
257 Alias for C<decode> to provide compatibility with C<TOML::Parser> when
258 overriding the parser by setting C<$TOML::Parser>.
261 =head1 DIFFERENCES FROM L<TOML> AND L<TOML::Parser>
263 C<TOML::Tiny> differs in a few significant ways from the L<TOML> module,
264 particularly in adding support for newer C<TOML> features and strictness.
266 L<TOML> defaults to lax parsing and provides C<strict_mode> to (slightly)
267 tighten things up. C<TOML::Tiny> defaults to (somehwat) stricter parsing, with
268 the exception of permitting heterogenous arrays (illegal in v4 and v5, but
269 permissible in the upcoming v6); optional enforcement of homogenous arrays is
270 supported with C<strict_arrays>.
272 C<TOML::Tiny> supports a number of options which do not exist in L<TOML>:
273 L</inflate_integer>, L</inflate_float>, and L</strict_arrays>.
275 C<TOML::Tiny> ignores invalid surrogate pairs within basic and multiline
276 strings (L<TOML> may attempt to decode an invalid pair). Additionally, only
277 those character escapes officially supported by TOML are interpreted as such by
280 C<TOML::Tiny> supports stripping initial whitespace and handles lines
281 terminating with a backslash correctly in multilne strings:
294 {x => 'foo', y => 'how now brown bureaucrat.'}
296 C<TOML::Tiny> includes support for integers specified in binary, octal or hex
297 as well as the special float values C<inf> and C<nan>.
303 =item L<TOML::Tiny::Grammar>
305 Regexp scraps used by C<TOML::Tiny> to parse TOML source.
309 =head1 ACKNOWLEDGEMENTS
311 Thanks to L<ZipRecruiter|https://www.ziprecruiter.com> for encouraging their
312 employees to contribute back to the open source ecosystem. Without their
313 dedication to quality software development this distribution would not exist.