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. | [reply] [Watch: Dir/Any] [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] [Watch: Dir/Any] |
|
[ 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. | [reply] [Watch: Dir/Any] [d/l] |
|
|
|
|
|
>
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] [Watch: Dir/Any] |
|
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. | [reply] [Watch: Dir/Any] [d/l] [select] |
|
|
|
Re^3: Shouldn't references be readonly?
by haj (Vicar) 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"?
| [reply] [Watch: Dir/Any] |
|
> 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] [Watch: Dir/Any] |
|
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"
| [reply] [Watch: Dir/Any] [d/l] |
|
|
|
|
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$
| [reply] [Watch: Dir/Any] [d/l] |
|
|
|