Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recognise core booleans on Perl 5.36+ at dump time (Fixes #113) #114

Merged
merged 2 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions LibYAML/perl_libyaml.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include <perl_libyaml.h>

#if (PERL_REVISION > 5) || (PERL_REVISION == 5 && PERL_VERSION >= 36)
# define PERL_HAVE_BOOLEANS
#endif

static SV *
call_coderef(SV *code, AV *args)
{
Expand Down Expand Up @@ -1152,12 +1156,20 @@ dump_scalar(perl_yaml_dumper_t *dumper, SV *node, yaml_char_t *tag)
string_len = 1;
style = YAML_PLAIN_SCALAR_STYLE;
}
else if (node == &PL_sv_yes) {
else if (node == &PL_sv_yes
#ifdef PERL_HAVE_BOOLEANS
|| (SvIsBOOL(node) && SvTRUE(node))
#endif
) {
string = "true";
string_len = 4;
style = YAML_PLAIN_SCALAR_STYLE;
}
else if (node == &PL_sv_no) {
else if (node == &PL_sv_no
#ifdef PERL_HAVE_BOOLEANS
|| (SvIsBOOL(node) && !SvTRUE(node))
#endif
) {
string = "false";
string_len = 5;
style = YAML_PLAIN_SCALAR_STYLE;
Expand Down
22 changes: 21 additions & 1 deletion test/boolean.t
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use FindBin '$Bin';
use lib $Bin;
use TestYAMLTests tests => 5;
use constant HAVE_BOOLEANS => ($^V ge v5.36);
use TestYAMLTests tests => 5 + (HAVE_BOOLEANS ? 2 : 0);

my $yaml = <<'...';
---
Expand Down Expand Up @@ -39,3 +40,22 @@ my $yaml4 = Dump Load $yaml3;

is $yaml4, $yaml3,
"Everything related to boolean YNY roundtrips";

if( HAVE_BOOLEANS ) {
no if HAVE_BOOLEANS, warnings => "experimental::builtin";

is Dump({ true => builtin::true, false => builtin::false }),
<<'...',
---
'false': false
'true': true
...
'core booleans dump as booleans';

ok builtin::is_bool(Load(<<'...',)->{false}),
---
'false': false
'true': true
...
'booleans loaded as core booleans';
}
Loading