Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^3: Shouldn't references be readonly?

by haj (Curate)
on Aug 05, 2020 at 14:59 UTC ( #11120321=note: print w/replies, xml ) Need Help??


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

For the same reason why it's prevented with other readonly values like literal scalars ??

As you have demonstrated, anonymous references are not readonly, so Perl does not prevent them from being overwritten. I still don't see a compelling reason why they should be read-only. Can you provide an example for "catching bugs with aliases"?

  • Comment on Re^3: Shouldn't references be readonly?

Replies are listed 'Best First'.
Re^4: Shouldn't references be readonly?
by LanX (Cardinal) on Aug 05, 2020 at 15:54 UTC
    > 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

      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

      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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11120321]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2020-12-03 02:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you use taint mode?





    Results (49 votes). Check out past polls.

    Notices?