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

Re: No implicit coercion?

Thread Previous | Thread Next
From:
Oodler 577 via perl5-porters
Date:
December 27, 2021 20:30
Subject:
Re: No implicit coercion?
Message ID:
YcoiVxjLg2nEtPTy@odin.sdf-eu.org
Not commenting on this specific issue, but some
time ago, there was a document collecting perl
idiosyncracies; maybe this should be added?

Maybe the recent thing related to "state" should
also be. I think there have been others recently
that fall into this category.

Cheers,
Brett

* Ovid via perl5-porters <perl5-porters@perl.org> [2021-12-27 18:35:05 +0000]:

> 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.
> 
> Best,
> Ovid
> -- 
> IT consulting, training, specializing in Perl, databases, and agile development
> http://www.allaroundtheworld.fr/. 
> 
> Buy my book! - http://bit.ly/beginning_perl
> 

-- 
--
oodler@cpan.org
oodler577@sdf-eu.org
SDF-EU Public Access UNIX System - http://sdfeu.org
irc.perl.org #openmp #pdl #native

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