Re^2: Shouldn't references be readonly?
by LanX (Saint) on Aug 05, 2020 at 11:41 UTC
|
[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.
| [reply] [d/l] |
|
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. | [reply] [d/l] |
|
> 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.
| [reply] |
|
|
|
|
|
>
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
| [reply] |
|
|
|
|
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"?
| [reply] |
|
> 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.
| [reply] |
|
|
|
|
|
|