http://qs321.pair.com?node_id=11120331


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

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.

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

        My complaint is primarily about aliases, where I don't understand why Perl complains about when overwriting an alias to 1 but not one to [ 1,2,3,$x] (see examples in my OP especially the update)

        Both are literal constructs unbound to any named variable.

        I'm NOT saying that anonymous arrays -ie the content - should be immutable, but their reference.

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

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

        after rereading it, yes this follows my mental model.

        Analog to

        1; # a readonly temporary scalar $a = 1 ; # $a is a rw copy of the temp scalar

        Of course I could be wrong, but I'm still waiting for someone to understand my question...

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

Re^4: Shouldn't references be readonly?
by LanX (Saint) on Aug 05, 2020 at 18:30 UTC
    > 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.