develooper Front page | perl.perl5.porters | Postings from January 2011

Re: Silent mutation when using in numeric context (was: Using smart matching...)

Thread Previous | Thread Next
From:
Leon Timmermans
Date:
January 26, 2011 08:58
Subject:
Re: Silent mutation when using in numeric context (was: Using smart matching...)
Message ID:
AANLkTi=7DkY_db=peqvkPnzVfSSrVor_fypH6c6CsnPv@mail.gmail.com
On Wed, Jan 26, 2011 at 5:46 PM, Ed Avis <eda@waniasset.com> wrote:
> This oddity leads to an example Readonly changing the semantics of a program:
>
>  my $b = "abc100";
>  $a = "abc100"; 0 + $b; $c = $b; $a ++; $c++; print $c;
>
> produces '1', whereas
>
>  use Readonly;
>  Readonly my $b => "abc100";
>  $a = "abc100"; 0 + $b; $c = $b; $a ++; $c++; print $c;
>
> produces 'abc101'.
>
> That must count as a bug either in Readonly or in perl.  Either $b is modified
> or it is not.  If it is not modified, then marking it readonly should make no
> difference.  If it is modified, then the Readonly version should fail with a
> run time or compile time error at the point where modification is attempted.

I can explain that one for you. Readonly uses ties (and most of the
time it does so even then Readonly::XS is installed). Under the hood
tie reassigns the value to $b on every read, so the effects of 0 + $b
gets annihilated. Readonly has many surprising edge cases, and its RT
queue contains a good overview of various bugs and issues with ties in
perl.

Leon

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