p5h / p5summit-2019

Perl 5 Summit
0 stars 0 forks source link

Length-or, please? #25

Closed FGasper closed 4 years ago

FGasper commented 4 years ago

Over the years I’ve not infrequently encountered this:

$form_value ||= $default

.. which undesirably rejects 0 as input.

Defined-or doesn’t help here because the form submits regardless.

It would be very helpful if Perl (7?) added a “length-or”: undef and q// are falsy, but everything else is truthy.

Thank you!

atoomic commented 4 years ago

we have ||=, //=, what about \\= :-)

leonerd commented 4 years ago

In a sense this points out the duality of the string-or-number model of Perl's type system in that is demonstrates the question of how you ask the "truth" of a value really depends on whether you wanted the stringy truth or the numerical truth.

# stringily false
undef, ""

# numerically false
undef, 0,  # plus "" which is exempt from number->string conversion warnings
FGasper commented 4 years ago

Precisely. Perl’s standard “or” makes sense when our scalars are numbers, but not when they’re strings.

atoomic commented 4 years ago

discussion moved to https://github.com/Perl/perl5/issues/17191