Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^4: Shouldn't references be readonly?

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


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

> 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

Replies are listed 'Best First'.
Re^5: Shouldn't references be readonly?
by hippo (Chancellor) on Aug 05, 2020 at 22:07 UTC
    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.


    🦛

Re^5: Shouldn't references be readonly?
by jcb (Vicar) on Aug 05, 2020 at 22:01 UTC
    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.
      you are aware that "constructor" isn't even mentioned in your definition? ;)

      calling [$a] a "literal constructor" is not far fetched.

      a non literal construction is in contrast to do { @a=($a); \@a } because it involves a variable.

      This it meets the definitions in perlglossary ("no variable") and is also mirrored in JS terminology which has 100% the same semantic.

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

        you are aware that "constructor" isn't even mentioned in your definition? ;)

        Yes, that is what I misremembered: perlref calls them "composers" but they act just like constructors. :-)

        Calling it a "constructor" is a hint that it does something at runtime, instead of being a literal embedded into the program text. That its return value is mutable should not be a surprise.

Log In?
Username:
Password:

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

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





    Results (41 votes). Check out past polls.

    Notices?