in reply to Re: Hash value test of zero
in thread Hash value test of zero

As the above code shows the "0 but true" exception is hard-coded into Perl as an exception. This allows a function to return a single value that can represent both logically true and numerically zero. Using that string as a numeric value will not cause a warning. This is now seldom seen as the exponential value 0E0 is more commonly used, especially by the DBI.

Interesting. From perlop:

Perl operators that return true or false generally return values that can be safely used as numbers. For example, the relational operators in this section and the equality operators in the next one return 1 for true and a special version of the defined empty string, "", which counts as a zero but is exempt from warnings about improper numeric conversions, just as "0 but true" is.

In my experience, this is well-known ... well much better known than the quirky dualvar ... which caused a sensation here recently when uncorked by marioroy during the long running Long List is Long saga. Mario is the only Perl programmer I know who has used dualvar in production code (in MCE::Shared::Cache part of MCE).

See also:

Replies are listed 'Best First'.
Re^3: Hash value test of zero
by tobyink (Canon) on Jul 26, 2023 at 10:39 UTC

    That perlop quote is a little out of date. It's not wrong per-se, but the boolean values returned by most operators are now more succinctly described as builtin::true and builtin::false.

      Interesting. First time I've heard of builtin::true and builtin::false ... I see perldoc builtin warns:

      The overall builtin mechanism, as well as every individual function it provides, are currently experimental

Re^3: Hash value test of zero
by NERDVANA (Priest) on Jul 27, 2023 at 05:14 UTC
    Mario is the only Perl programmer I know who has used dualvar in production code

    Well now you know two, because I delight in using dualvars for XS constants :-)

    use Fluent::LibFluentBit qw( FLB_LIB_NO_CONFIG_MAP ); say FLB_LIB_NO_CONFIG_MAP; say 0+FLB_LIB_NO_CONFIG_MAP;
    Output:
    FLB_LIB_NO_CONFIG_MAP 2

    The great part is that when you pass them to XS functions, they pass the integer that XS is looking for, and when you dump those parameters to STDOUT or a logger, you get the constant name so you can understand what is going on.