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

Re: No implicit coercion?

Thread Previous | Thread Next
From:
Felipe Gasper
Date:
December 27, 2021 18:42
Subject:
Re: No implicit coercion?
Message ID:
81948B1F-1A9F-4836-9194-F466D7C0A42D@felipegasper.com

> On Dec 27, 2021, at 13:35, Ovid via perl5-porters <perl5-porters@perl.org> wrote:
> 
> Hi all,
> 
> This is not a pre-RFC, but I think most of us have been bitten at times by bugs like this:
> 
>     my @values = (in => "data.csv");
>     $values[2]++;
> 
> Except that $values[2] was the string "n/a" and now it's "1".
> 
> The core of the idea is simple. it would be lovely to have something to prevent implicit coercion in a given lexical scope:
> 
>     use explicit;
>     my $num = 3;   # integer
>     $num += .42;    # fatal because it creates a float
> 
> Instead, we have to do this:
> 
>     use explicit;
>     my $num = 4;
>     $num = float($num);
>     # or `$num = 4.0;`
>     $num += .42; # works just fine
> 
> This would also be fatal:
> 
>     use explicit;
>     my $value = {};
>     say ++$value;
> 
> Is this possible? I realize there are many edge cases.

+1

IMO a “stricter” mode that refuses to do “nonsensical” things like numeric operations on non-numeric SVs, scalar ops on references, etc. would be one of the most helpful features Perl could add. (Magic notwithstanding, of course.)

We all know Perl “happily confuses” numbers and numeric strings, but other cases seem like places where the language could (fairly?) readily assist with bug-hunting.

Is there a list anywhere of such behaviours?

-F
Thread Previous | Thread Next


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