http://www.perlmonks.org?node_id=11120327


in reply to Re^3: Shouldn't references be readonly?
in thread Shouldn't LITERAL references be readonly? (updated)

> compelling reason why they should be read-only.

Again, what is the compelling reason to throw an error with aliased 1 or "string" or undef???

The OP has an example.

Same league, it's not more logical at all.

Probably some implementation detail related to magic edge-cases like auto-vivification or so.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^5: Shouldn't references be readonly?
by haj (Vicar) on Aug 05, 2020 at 17:58 UTC

    I doubt it for the Perl interpreter, but I do know why compilers I've worked with treat numeric and string literals as constants: Optimization.

    Constant literals can be packed tightly, like sardines in a box. If you have the string literal "foo" in several places of your program, all of them can point to the same memory location. This would break, of course, if aliasing (or call-by-reference in subroutines) were able to change the values.

    References, on the other hand, can never be optimized like this: Two anonymous instances of [] need to be able to lead different lives because you might want to push different values into them.

    So there is a reason why a compiler (or interpreter) might want to treat string and number literals as constants, a reason which does not exist for anonymous references.

    BTW: Perl's constant handling is only skin-deep:

    $ perl -MData::Dump=dump -E 'dump map { $_ = 'boo' } ( 0 )' Modification of a read-only value attempted at -e line 1. $ perl -MData::Dump=dump -E 'dump map { $_ = 'boo' } ( 0+0 )' "boo"
      > Two anonymous instances of [] need to be able to lead different lives

      Yes, but in this case we are talking about aliases which have the same lives.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        ENOCONTEXT

        I have no idea about which aliases you are talking here, and how aliases refer to my text.

        But anyway: I've read your replies to other articles and got the impression that this is just for the fun of debate - I'm leaving this thread now.

Re^5: Shouldn't references be readonly?
by jo37 (Deacon) on Aug 05, 2020 at 18:11 UTC

    Isn't an alias something dereferenced into a symbol table entry? So a reference and an alias are on different levels of indirection.

    DB<1> use Scalar::Util 'readonly' DB<2> p readonly 1 134217728 DB<3> p readonly \1 0 DB<4> *one = \1 DB<5> p readonly $one 134217728 DB<6> $rone = \1 DB<7> p readonly $$rone 134217728 DB<8> $one = 2 Modification of a read-only value attempted at (eval 16)[/usr/share/pe +rl/5.20/perl5db.pl:732] line 2. DB<9> $$rone = 2 Modification of a read-only value attempted at (eval 17)[/usr/share/pe +rl/5.20/perl5db.pl:732] line 2.

    Greetings,
    -jo

    $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$
      • alias

        A nickname for something, which behaves in all ways as though you’d used the original name instead of the nickname. Temporary aliases are implicitly created in the loop variable for foreach loops, in the $_ variable for map or grep operators, in $a and $b during sort’s comparison function, and in each element of @_ for the actual arguments of a subroutine call. Permanent aliases are explicitly created in packages by importing symbols or by assignment to typeglobs. Lexically scoped aliases for package variables are explicitly created by the our declaration.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        This a lame description of aliases. See Symbol Tables.

        Greetings,
        -jo

        $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$