1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
|
package TOML::Tiny;
# ABSTRACT: a minimal, pure perl TOML parser and serializer
$TOML::Tiny::VERSION = '0.20';
use strict;
use warnings;
no warnings qw(experimental);
use v5.18;
use TOML::Tiny::Parser ();
use TOML::Tiny::Writer ();
use parent 'Exporter';
our @EXPORT = qw(
from_toml
to_toml
);
#-------------------------------------------------------------------------------
# TOML module compatibility
#-------------------------------------------------------------------------------
sub from_toml {
my ($source, %param) = @_;
# strict was previously strict_arrays; accept both for backward
# compatibility.
if (exists $param{strict_arrays}) {
$param{strict} = $param{strict_arrays};
delete $param{strict_arrays};
}
my $parser = TOML::Tiny::Parser->new(%param);
my $toml = eval{ $parser->parse($source) };
if (wantarray) {
return ($toml, $@);
} else {
die $@ if $@;
return $toml;
}
}
sub to_toml {
my ($data, %param) = @_;
# strict was previously strict_arrays; accept both for backward
# compatibility.
if (exists $param{strict_arrays}) {
$param{strict} = $param{strict_arrays};
delete $param{strict_arrays};
}
TOML::Tiny::Writer::to_toml($data, %param);
}
#-------------------------------------------------------------------------------
# Object API
#-------------------------------------------------------------------------------
sub new {
my ($class, %param) = @_;
bless{ %param, parser => TOML::Tiny::Parser->new(%param) }, $class;
}
sub decode {
my ($self, $source) = @_;
$self->{parser}->parse($source);
}
sub encode {
my ($self, $data) = @_;
TOML::Tiny::Writer::to_toml(
$data,
strict => $self->{strict},
no_string_guessing => $self->{no_string_guessing},
);
}
#-------------------------------------------------------------------------------
# For compatibility with TOML::from_toml's use of $TOML::Parser
#-------------------------------------------------------------------------------
sub parse {
goto \&decode;
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
TOML::Tiny - a minimal, pure perl TOML parser and serializer
=head1 VERSION
version 0.20
=head1 SYNOPSIS
use TOML::Tiny qw(from_toml to_toml);
binmode STDIN, ':encoding(UTF-8)';
binmode STDOUT, ':encoding(UTF-8)';
# Decoding TOML
my $toml = do{ local $/; <STDIN> };
my ($parsed, $error) = from_toml $toml;
# Encoding TOML
say to_toml({
stuff => {
about => ['other', 'stuff'],
},
});
# Object API
my $parser = TOML::Tiny->new;
my $data = $parser->decode($toml);
say $parser->encode($data);
=head1 DESCRIPTION
=for html <p>
<a href="https://github.com/sysread/TOML-Tiny/actions?query=workflow%3Arun-tests">
<img src="https://github.com/sysread/TOML-Tiny/workflows/run-tests/badge.svg" alt="Build status" />
</a>
</p>
C<TOML::Tiny> implements a pure-perl parser and generator for the
L<TOML|https://github.com/toml-lang/toml> data format. It conforms to TOML v1.0
(with a few caveats; see L</strict>).
C<TOML::Tiny> strives to maintain an interface compatible to the L<TOML> and
L<TOML::Parser> modules, and could even be used to override C<$TOML::Parser>:
use TOML;
use TOML::Tiny;
local $TOML::Parser = TOML::Tiny->new(...);
say to_toml(...);
=head1 EXPORTS
C<TOML::Tiny> exports the following to functions for compatibility with the
L<TOML> module. See L<TOML/FUNCTIONS>.
=head2 from_toml
Parses a string of C<TOML>-formatted source and returns the resulting data
structure. Any arguments after the first are passed to L<TOML::Tiny::Parser>'s
constructor.
If there is a syntax error in the C<TOML> source, C<from_toml> will die with
an explanation which includes the line number of the error.
my $result = eval{ from_toml($toml_string) };
Alternately, this routine may be called in list context, in which case syntax
errors will result in returning two values, C<undef> and an error message.
my ($result, $error) = from_toml($toml_string);
Additional arguments may be passed after the toml source string; see L</new>.
=head3 GOTCHAS
=over
=item Big integers and floats
C<TOML> supports integers and floats larger than what many perls support. When
C<TOML::Tiny> encounters a value it may not be able to represent as a number,
it will instead return a L<Math::BigInt> or L<Math::BigFloat>. This behavior
can be overridden by providing inflation routines:
my $toml = TOML::Tiny->new(
inflate_float => sub{
return do_something_else_with_floats( $_[0] );
};
);
=back
=head2 to_toml
Encodes a hash ref as a C<TOML>-formatted string.
my $toml = to_toml({foo => {'bar' => 'bat'}});
# [foo]
# bar="bat"
=head3 mapping perl to TOML types
=head4 table
=over
=item C<HASH> ref
=back
=head4 array
=over
=item C<ARRAY> ref
=back
=head4 boolean
=over
=item C<\0> or C<\1>
=item L<JSON::PP::Boolean>
=item L<Types::Serializer::Boolean>
=back
=head4 numeric types
These are tricky in perl. When encountering a C<Math::Big[Int|Float]>,
that representation is used.
If the value is a defined (non-ref) scalar with the C<SVf_IOK> or C<SVf_NOK>
flags set, the value will be emitted unchanged. This is in line with most
other packages, so the normal hinting hacks for typed output apply:
number => 0 + $number,
string => "" . $string,
=over
=item L<Math::BigInt>
=item L<Math::BigFloat>
=item numerical scalars
=back
=head4 datetime
=over
=item RFC3339-formatted string
e.g., C<"1985-04-12T23:20:50.52Z">
=item L<DateTime>
L<DateTime>s are formatted as C<RFC3339>, as expected by C<TOML>. However,
C<TOML> supports the concept of a "local" time zone, which strays from
C<RFC3339> by allowing a datetime without a time zone offset. This is
represented in perl by a C<DateTime> with a B<floating time zone>.
=back
=head4 string
All other non-ref scalars are treated as strings.
=head1 OBJECT API
=head2 new
=over
=item inflate_datetime
By default, C<TOML::Tiny> treats TOML datetimes as strings in the generated
data structure. The C<inflate_datetime> parameter allows the caller to provide
a routine to intercept those as they are generated:
use DateTime::Format::RFC3339;
my $parser = TOML::Tiny->new(
inflate_datetime => sub{
my ($dt_string) = @_;
# DateTime::Format::RFC3339 will set the resulting DateTime's formatter
# to itself. Fallback is the DateTime default, ISO8601, with a possibly
# floating time zone.
return eval{ DateTime::Format::RFC3339->parse_datetime($dt_string) }
|| DateTime::Format::ISO8601->parse_datetime($dt_string);
},
);
=item inflate_boolean
By default, boolean values in a C<TOML> document result in a C<1> or C<0>.
If L<Types::Serialiser> is installed, they will instead be C<Types::Serialiser::true>
or C<Types::Serialiser::false>.
If you wish to override this, you can provide your own routine to generate values:
my $parser = TOML::Tiny->new(
inflate_boolean => sub{
my $bool = shift;
if ($bool eq 'true') {
return 'The Truth';
} else {
return 'A Lie';
}
},
);
=item inflate_integer
TOML integers are 64 bit and may not match the size of the compiled perl's
internal integer type. By default, C<TOML::Tiny> coerces numbers that fit
within a perl number by adding C<0>. For bignums, a L<Math::BigInt> is
returned. This may be overridden by providing an inflation routine:
my $parser = TOML::Tiny->new(
inflate_integer => sub{
my $parsed = shift;
return sprintf 'the number "%d"', $parsed;
};
);
=item inflate_float
TOML floats are 64 bit and may not match the size of the compiled perl's
internal float type. As with integers, floats are coerced to numbers and large
floats are upgraded to L<Math::BigFloat>s. The special strings C<NaN> and
C<inf> may also be returned. You can override this by specifying an inflation
routine.
my $parser = TOML::Tiny->new(
inflate_float => sub{
my $parsed = shift;
return sprintf '"%0.8f" is a float', $parsed;
};
);
=item strict
C<strict> imposes some miscellaneous strictures on C<TOML> input, such as
disallowing trailing commas in inline tables and failing on invalid UTF8 input.
B<Note:> C<strict> was previously called C<strict_arrays>. Both are accepted
for backward compatibility, although enforcement of homogenous arrays is no
longer supported as it has been dropped from the spec.
=back
=head2 decode
Decodes C<TOML> and returns a hash ref. Dies on parse error.
=head2 encode
Encodes a perl hash ref as a C<TOML>-formatted string.
=head2 parse
Alias for C<decode> to provide compatibility with C<TOML::Parser> when
overriding the parser by setting C<$TOML::Parser>.
=head1 DIFFERENCES FROM L<TOML> AND L<TOML::Parser>
C<TOML::Tiny> differs in a few significant ways from the L<TOML> module,
particularly in adding support for newer C<TOML> features and strictness.
L<TOML> defaults to lax parsing and provides C<strict_mode> to (slightly)
tighten things up. C<TOML::Tiny> defaults to (somewhat) stricter parsing,
enabling some extra strictures with L</strict>.
C<TOML::Tiny> supports a number of options which do not exist in L<TOML>:
L</inflate_integer>, L</inflate_float>, and L</strict>.
C<TOML::Tiny> ignores invalid surrogate pairs within basic and multiline
strings (L<TOML> may attempt to decode an invalid pair). Additionally, only
those character escapes officially supported by TOML are interpreted as such by
C<TOML::Tiny>.
C<TOML::Tiny> supports stripping initial whitespace and handles lines
terminating with a backslash correctly in multilne strings:
# TOML input
x="""
foo"""
y="""\
how now \
brown \
bureaucrat.\
"""
# Perl output
{x => 'foo', y => 'how now brown bureaucrat.'}
C<TOML::Tiny> includes support for integers specified in binary, octal or hex
as well as the special float values C<inf> and C<nan>.
=head1 SEE ALSO
=over
=item L<TOML::Tiny::Grammar>
Regexp scraps used by C<TOML::Tiny> to parse TOML source.
=back
=head1 ACKNOWLEDGEMENTS
Thanks to L<ZipRecruiter|https://www.ziprecruiter.com> for encouraging their
employees to contribute back to the open source ecosystem. Without their
dedication to quality software development this distribution would not exist.
A big thank you to those who have contributed code or bug reports:
=over
=item L<ijackson|https://github.com/ijackson>
=item L<noctux|https://github.com/noctux>
=item L<oschwald|https://github.com/oschwald>
=item L<jjatria|https://metacpan.org/author/JJATRIA>
=back
=head1 AUTHOR
Jeff Ober <sysread@fastmail.fm>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2025 by Jeff Ober.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|