in reply to Re^2: Why is Dumper returning "!!1" for true file test?
in thread Why is Dumper returning "!!1" for true file test?

I hadn't heard about the new booleans.

They're not new. They've existed for a very long time. At least since Perl 5.6 in 2000, but probably long before that. What was missing were functions whose sole purpose was to return them. That's what was recently introduced. And that's why we used !!0 and !!1 to produce them before that.

Replies are listed 'Best First'.
Re^4: Why is Dumper returning "!!1" for true file test?
by Haarg (Priest) on Jan 22, 2024 at 15:14 UTC

    Perl 5.36 does have "new" booleans. Copies of the immortal &PL_sv_yes and &PL_sv_no values are still identifiable as booleans. This means they can be serialized more accurately. The latest versions of JSON::PP, Data::Dumper, and Storable all support this.

    Prior to perl 5.36 it was not possible to identify a boolean true value after it had been copied. The exact same SV type, flags, and values could be produced by normal usage of a string "1". A boolean false would be more identifiable as it would never be produced naturally, requiring dualvar to construct.

      Prior to perl 5.36 it was not possible to identify a boolean true value after it had been copied

      This was done by ensuring that the string buffer (PV) of &PL_sv_yes and &PL_sv_no is shared on assignment.

      $ 5.34t/bin/perl -MDevel::Peek -e'Dump(!!1); my $x = !!1; Dump($x);' SV = PVNV(0x55852a177160) at 0x55852a1753c8 REFCNT = 2147483644 FLAGS = (IOK,NOK,POK,READONLY,PROTECT,pIOK,pNOK,pPOK) IV = 1 NV = 1 PV = 0x558529a84f64 "1" CUR = 1 LEN = 0 SV = PVNV(0x55852a177220) at 0x55852a178770 REFCNT = 1 FLAGS = (IOK,NOK,POK,pIOK,pNOK,pPOK) IV = 1 NV = 1 PV = 0x55852a1b6010 "1"\0 CUR = 1 LEN = 10 $ 5.36t/bin/perl -MDevel::Peek -e'Dump(!!1); my $x = !!1; Dump($x);' SV = PVNV(0x55d55f1be160) at 0x55d55f1bc3c8 REFCNT = 2147483644 FLAGS = (IOK,NOK,POK,IsCOW,READONLY,PROTECT,pIOK,pNOK,pPOK) IV = 1 NV = 1 PV = 0x55d55e89b984 "1" [BOOL PL_Yes] CUR = 1 LEN = 0 SV = PVNV(0x55d55f1be220) at 0x55d55f1bf770 REFCNT = 1 FLAGS = (IOK,NOK,POK,IsCOW,pIOK,pNOK,pPOK) IV = 1 NV = 1 PV = 0x55d55e89b984 "1" [BOOL PL_Yes] CUR = 1 LEN = 0

      is_bool checks if the provided scalar shares a string buffer (PV) with &PL_sv_yes or &PL_sv_no.

      Before you had two "levels" of true:

      • &PL_sv_yes
      • true or false value

      After, three:

      • &PL_sv_yes
      • is_bool
      • true or false value
Re^4: Why is Dumper returning "!!1" for true file test?
by LanX (Saint) on Jan 22, 2024 at 09:31 UTC
    > What was missing is a function whose sole purpose was to return them. That's what was recently introduced. And that's why we used !!0 and !!1 to produce them before that.

    Are you referring to builtin::true etc. in builtin? Nobody has mentioned them yet in this thread.

    Or are you talking about the internal &PL_sv_yes ?

    On a side note: shouldn't it be possible to activate true and false while silencing the warnings with just one pragma?

    use experimental qw/builtin/; use builtin qw/true false/; use Data::Dump; ddx $]; dd [true,false] __END__ # bool.pl:6: "5.036000" [1, ""]

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

      Them, Perl's booleans = &PL_sv_yes and &PL_sv_no.

      Recently introduced functions = builtin::true and builtin::false