Entries using [ ] and [[ ]] change the default context for
assignments. So we must put all the bare assigments (using `=')
(and plain values) first.
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
sub to_toml {
my $data = shift;
my %param = @_;
sub to_toml {
my $data = shift;
my %param = @_;
+ my @buff_assign;
+ my @buff_tables;
for (ref $data) {
when ('HASH') {
for (ref $data) {
when ('HASH') {
for my $k (grep{ ref($data->{$_}) !~ /HASH|ARRAY/ } sort keys %$data) {
my $key = to_toml_key($k);
my $val = to_toml($data->{$k}, %param);
for my $k (grep{ ref($data->{$_}) !~ /HASH|ARRAY/ } sort keys %$data) {
my $key = to_toml_key($k);
my $val = to_toml($data->{$k}, %param);
- push @buff, "$key=$val";
+ push @buff_assign, "$key=$val";
}
# For values which are arrays, generate inline arrays for non-table
}
# For values which are arrays, generate inline arrays for non-table
# Empty table
if (!@{$data->{$k}}) {
my $key = to_toml_key($k);
# Empty table
if (!@{$data->{$k}}) {
my $key = to_toml_key($k);
+ push @buff_assign, "$key=[]";
if (@inline) {
my $key = to_toml_key($k);
my $val = to_toml(\@inline, %param);
if (@inline) {
my $key = to_toml_key($k);
my $val = to_toml(\@inline, %param);
- push @buff, "$key=$val";
+ push @buff_assign, "$key=$val";
}
# Table values become an array-of-tables
}
# Table values become an array-of-tables
push @KEYS, $k;
for (@table_array) {
push @KEYS, $k;
for (@table_array) {
- push @buff, '', '[[' . join('.', map{ to_toml_key($_) } @KEYS) . ']]';
- push @buff, to_toml($_);
+ push @buff_tables, '', '[[' . join('.', map{ to_toml_key($_) } @KEYS) . ']]';
+ push @buff_tables, to_toml($_);
if (!keys(%{$data->{$k}})) {
# Empty table
my $key = to_toml_key($k);
if (!keys(%{$data->{$k}})) {
# Empty table
my $key = to_toml_key($k);
+ push @buff_assign, "$key={}";
} else {
# Generate [table]
push @KEYS, $k;
} else {
# Generate [table]
push @KEYS, $k;
- push @buff, '', '[' . join('.', map{ to_toml_key($_) } @KEYS) . ']';
- push @buff, to_toml($data->{$k}, %param);
+ push @buff_tables, '', '[' . join('.', map{ to_toml_key($_) } @KEYS) . ']';
+ push @buff_tables, to_toml($data->{$k}, %param);
die "toml: found heterogenous array, but strict_arrays is set ($err)\n" unless $ok;
}
die "toml: found heterogenous array, but strict_arrays is set ($err)\n" unless $ok;
}
- push @buff, '[' . join(', ', map{ to_toml($_, %param) } @$data) . ']';
+ push @buff_tables, '[' . join(', ', map{ to_toml($_, %param) } @$data) . ']';
} elsif ($$_ eq '0') {
return 'false';
} else {
} elsif ($$_ eq '0') {
return 'false';
} else {
- push @buff, to_toml($$_, %param);
+ push @buff_assign, to_toml($$_, %param);
+ join "\n", @buff_assign, @buff_tables;