Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: Shouldn't references be readonly?

by LanX (Cardinal)
on Aug 05, 2020 at 11:41 UTC ( #11120317=note: print w/replies, xml ) Need Help??


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

[1,2,3] is called a literal array in many languages like for instance JavaScript.

Since Perl has both an explicit $reference and a @list form of its data structures, it might be difficult to point out "the" literal form.

> I also fail to see why Perl should prevent it.

For the same reason why it's prevented with other readonly values like literal scalars ... to catch bugs with aliases.

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

Replies are listed 'Best First'.
Re^3: Shouldn't references be readonly?
by dave_the_m (Monsignor) on Aug 05, 2020 at 17:33 UTC
    Perl doesn't have literal arrays. It has constructors which will create a reference to an anonymous array, where the elements of that array are initially assigned to by copying the values from a list which may or may not be literal. E.g.
    $ar1 = [ 1,2,3 ]; $ar2 = [ 1,2,3,$four ];
    Should @$ar1 and @$ar2 be treated differently, and if so, why?

    Dave.

      > Should @$ar1 and @$ar2 be treated differently,

      No, but what's the point to allow overwriting an alias for references without throwing an error?

      Should they be treated differently than 42, undef and "str", and if so, why?

      Again, I'm talking about the container, not the mutable content.

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

        Having had a further look at your OP, you seem to be complaining that the scalar ref value should readonly - I was thinking that you were wanting the anonymous array to readonly?

        So is the following a correct summary of your position as to how you would like things to work?

        [ 1, 2, 3]; # a readonly temporary reference to a mutable anonymous a +rray [ 1, 2, $x]; # also a readonly temporary reference to a mutable anonym +ous array bless 'Foo', [1,2,3];# a readonly temporary reference to a mutable ano +nymous array # where the array was mutated by blessing it $a = [1, 2, 3]; # $a is a rw copy of the temp ro array reference

        Dave.

      > Perl doesn't have literal arrays ... a reference to an anonymous array,

      Perlglossary

      literal

      A token in a programming language, such as a number or string, that gives you an actual value instead of merely representing possible values as a variable does.

      anonymous

      Used to describe a referent that is not directly accessible through a named variable. Such a referent must be indirectly accessible through at least one hard reference. When the last hard reference goes away, the anonymous referent is destroyed without pity

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

        Which of the following do you regard as a literal array:
        [] [1,2,3] [1,2,$x]
        I regard none of them as literal. [] {} are constructors; they are just syntactic sugar for a function which takes a list and returns a reference to an anonymous aggregate, e.g.
        [1,2,3] # is the same as anon_array(1,2,3); sub anon_array { my @a = @_; \@a }

        Dave.

Re^3: Shouldn't references be readonly?
by haj (Curate) on Aug 05, 2020 at 14:59 UTC
    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"?

      > 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"

        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$

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2020-11-23 16:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?