develooper Front page | perl.perl5.porters | Postings from December 2021

Re: Pre-RFC: `unknown` versus `undef`

Thread Previous
From:
Dan Book
Date:
December 19, 2021 16:12
Subject:
Re: Pre-RFC: `unknown` versus `undef`
Message ID:
CABMkAVXdaPvD1CVSn2=6eQ4jvE30nxwebKR=we+vFgqou5QA4Q@mail.gmail.com
On Sun, Dec 19, 2021 at 11:09 AM Dan Book <grinnz@gmail.com> wrote:

> On Sun, Dec 19, 2021 at 7:14 AM Ovid via perl5-porters <
> perl5-porters@perl.org> wrote:
>
>> The if/else is actually pretty simple if we step back for a moment. I
>> think the confusion is that we misunderstand what an "else" block means in
>> Perl. Let's consider this:
>>
>>     if ( $var > 3 ) {
>>         ...
>>     }
>>     else {
>>         ...
>>     }
>>
>> In the above, in the else block, we mentally assume that "$var <= 3"
>> holds. In many statically typed languages, that assumption might hold true.
>>
>> In Perl, $var might be undef and be evaluated as less than three.
>> However, $var might be the string "Hello, World". $var might also be a
>> reference to a hash, we get absolutely no warning, and we hit our else
>> block with an assumption that is probably true ($var <= 3), but not in this
>> particular case. We _should_ be verifying what kind of data that $var
>> holds, but usually we don't.
>>
>
> But this isn't really what's going on here. In Perl, every scalar value is
> a number, once you use it as one. Hash references numify to their refaddr.
> So this comparison is still perfectly valid and there are no type
> conflicts, for any scalar value except those which die when numified.
>

Addendum: NaN and Inf, however, are "interesting" numbers, and NaN behaves
much like the proposed unknown in numeric comparisons: $nan > 3 and $nan <=
3 are both false, but it would hit the else block.

-Dan

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About