From 55bb403d54c58ca4402359c52605a869a5577d84 Mon Sep 17 00:00:00 2001 From: Slaven Rezic Date: Thu, 13 Mar 2025 14:19:36 +0100 Subject: [PATCH] fix bool handling in newer perls (RT #155819) The old readonly hack for detecting booleans does not work anymore for newer perls and newer YAML::XS versions. Use instead the builtin::is_bool function. --- lib/App/NDTools/Slurp.pm | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/App/NDTools/Slurp.pm b/lib/App/NDTools/Slurp.pm index 8aa652d..706f9df 100644 --- a/lib/App/NDTools/Slurp.pm +++ b/lib/App/NDTools/Slurp.pm @@ -9,7 +9,19 @@ use open qw(:std :utf8); use File::Basename qw(basename); use JSON qw(); -use Scalar::Util qw(readonly); +use if $] >= 5.035007, 'experimental' => 'builtin'; +use if $] >= 5.035007, 'builtin' => 'is_bool'; +use if $] <= 5.035007, 'Scalar::Util' => 'readonly'; + +BEGIN { + if (!defined &is_bool) { + # YAML::XS decode boolean vals as PL_sv_yes and PL_sv_no, both - read only + # at least until https://github.com/ingydotnet/yaml-libyaml-pm/issues/25 + # second thing here: get rid of dualvars: YAML::XS load numbers as + # dualvars, but JSON::XS dumps them as strings =( + *is_bool = sub ($) { Scalar::Util::readonly(shift) }; # pre 5.38 hack + } +} use App::NDTools::INC; use App::NDTools::Util qw(is_number); @@ -45,11 +57,6 @@ sub _decode_yaml($) { my $data = YAML::XS::Load($_[0]); - # YAML::XS decode boolean vals as PL_sv_yes and PL_sv_no, both - read only - # at least until https://github.com/ingydotnet/yaml-libyaml-pm/issues/25 - # second thing here: get rid of dualvars: YAML::XS load numbers as - # dualvars, but JSON::XS dumps them as strings =( - my @stack = (\$data); my $ref; @@ -58,7 +65,7 @@ sub _decode_yaml($) { for (0 .. $#{${$ref}}) { if (ref ${$ref}->[$_]) { push @stack, \${$ref}->[$_]; - } elsif (readonly ${$ref}->[$_]) { + } elsif (is_bool ${$ref}->[$_]) { splice @{${$ref}}, $_, 1, (${$ref}->[$_] ? TRUE : FALSE); } elsif (is_number ${$ref}->[$_]) { ${$ref}->[$_] += 0; @@ -68,7 +75,7 @@ sub _decode_yaml($) { for (keys %{${$ref}}) { if (ref ${$ref}->{$_}) { push @stack, \${$ref}->{$_}; - } elsif (readonly ${$ref}->{$_}) { + } elsif (is_bool ${$ref}->{$_}) { ${$ref}->{$_} = delete ${$ref}->{$_} ? TRUE : FALSE; } elsif (is_number ${$ref}->{$_}) { ${$ref}->{$_} += 0; -- 2.34.1