http://www.perlmonks.org?node_id=11120285

LanX has asked for the wisdom of the Perl Monks concerning the following question:

I stumbled over the fact that literal references are not readonly.

Is that a bug or am I missing an obscure reason for this???

DB<18> use Scalar::Util qw/readonly/ DB<19> p readonly 1 34283264 DB<20> p readonly undef 34283264 DB<21> p readonly {} # What? DB<22> p readonly [] # ... DB<23> p readonly bless([],"TEST") # ... DB<24> x [],{},bless([],"TEST") # all refs ARRAY(0x334ebd0) empty array HASH(0x334ec00) empty hash TEST=ARRAY(0x334eb70) empty array DB<25> x map { $_ = 1 } [],{},bless([],"TEST") # mutable! WHY??? 1 1 1 DB<26>

update

counter-examples

DB<32> x map { $_ = 1 } "str" Modification of a read-only value attempted at (eval 42)[c:/Perl_524/l +ib/perl5db.pl:737] line 2. DB<33> x map { $_ = 1 } 42 Modification of a read-only value attempted at (eval 43)[c:/Perl_524/l +ib/perl5db.pl:737] line 2. DB<34> x map { $_ = 1 } undef Modification of a read-only value attempted at (eval 44)[c:/Perl_524/l +ib/perl5db.pl:737] line 2.

update

see also Re^10: Shouldn't references be readonly? (UPDATED) for another example of the asymmetric behavior.

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