Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^5: Shouldn't references be readonly?

by haj (Curate)
on Aug 05, 2020 at 17:58 UTC ( #11120333=note: print w/replies, xml ) Need Help??


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

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"

Replies are listed 'Best First'.
Re^6: Shouldn't references be readonly?
by LanX (Cardinal) on Aug 05, 2020 at 18:03 UTC
    > Two anonymous instances of [] need to be able to lead different lives

    Yes, but in this case we are talking about aliases which have the same lives.

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

      ENOCONTEXT

      I have no idea about which aliases you are talking here, and how aliases refer to my text.

      But anyway: I've read your replies to other articles and got the impression that this is just for the fun of debate - I'm leaving this thread now.

        > I have no idea about which aliases you are talking here,

        In the OP in lines 25 -34 $_ is an alias.

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

        «...just for the fun of debate...»

        I’ve read this thread (and others) over and over again and jumped to the conclusion that this stuff is his obsession. It’s all so «Jesuitisch raffiniert» according to Herbert Wehner. Best regards, KGB

        «The Crux of the Biscuit is the Apostrophe»

        perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2020-12-02 13:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you use taint mode?





    Results (41 votes). Check out past polls.

    Notices?