Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: Shouldn't references be readonly?

by LanX (Cardinal)
on Aug 05, 2020 at 19:13 UTC ( #11120347=note: print w/replies, xml ) Need Help??


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

> The {} and [] operators construct new aggregates each time they are evaluated.

This might come as a surprise but literals in Perl are always new as you can see by the references.

DB<66> p \1 SCALAR(0x335c4a0) DB<67> p \1 SCALAR(0x335c6f8) DB<68> p \1 SCALAR(0x335cae8) DB<69>

I know there are languages where it's always the same (IMHO do Ruby and Lisp have a syntax for this) but this is not what I mean or asked.

Alas ... few of the respondents here seem to have read the code in the OP and really have a deep grasp of aliases.

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

) well undef is an exception, but it's debatable if that's a literal or a constant.

DB<69> p \undef SCALAR(0xfb9098) DB<70> p \undef SCALAR(0xfb9098) DB<71> p \undef SCALAR(0xfb9098) DB<72> p \undef SCALAR(0xfb9098) DB<73> p \"str" SCALAR(0x335d3c0) DB<74> p \"str" SCALAR(0x335d498) DB<75> p \"str" SCALAR(0x335d270)

Replies are listed 'Best First'.
Re^3: Shouldn't references be readonly?
by jcb (Vicar) on Aug 05, 2020 at 21:48 UTC

    That seems to be because a reference to a literal actually constructs a new scalar; note that the examples you gave where an error was thrown at an attempt to modify a read-only value did not involve references, but only aliases to literal values. I suspect that the anonymous scalar you get when you say \1 is probably modifiable. There is no such thing as a literal aggregate in Perl — the {} and [] operators are defined as constructors instead.

      > That seems to be because a reference to a literal actually constructs a new scalar;

      again more theories ...

      see how the ref to a real constant like a stays stable in Re^8: Shouldn't references be readonly? (updated) and please explain the difference to your allegedly constant 1 .

      > There is no such thing as a literal aggregate in Perl the {} and [] operators are defined as constructors instead.

      Defined? ... Source?

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

        please explain the difference to your allegedly constant 1 .

        Simple. Your constant a has a label. The label means that it uses a specific location to store the (constant) value. All refs to the label resolve to the location of the value and hence stringify to the same address.

        By contrast 1 has no label. It is an actual literal. This does not require one specific location to store it and therefore refs to it will refer to different locations.


        🦛

        Source?

        Source is perlref: (I misremembered slightly)

        Anonymous hash and array composers like these can be intermixed freely to produce as complicated a structure as you want. The multidimensional syntax described below works for these too. The values above are literals, but variables and expressions would work just as well, because assignment operators in Perl (even within local() or my()) are executable statements, not compile-time declarations.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2020-12-03 01:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you use taint mode?





    Results (49 votes). Check out past polls.

    Notices?