Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Shouldn't references be readonly?

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


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

My take on this: "Literal references" don't exist in Perl, at least they don't appear in perldata. A literal reference would be something like ARRAY(0x557782f0a4c8), but you can't feed this to Perl.

The constructs {...} and [...] are anonymous, but not literals.

I fail to see a point in clobbering anonymous variables, but then I also fail to see why Perl should prevent it.

Replies are listed 'Best First'.
Re^2: Shouldn't references be readonly?
by LanX (Cardinal) on Aug 05, 2020 at 11:41 UTC
    [1,2,3] is called a literal array in many languages like for instance JavaScript.

    Since Perl has both an explicit $reference and a @list form of its data structures, it might be difficult to point out "the" literal form.

    > I also fail to see why Perl should prevent it.

    For the same reason why it's prevented with other readonly values like literal scalars ... to catch bugs with aliases.

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

      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.

        > 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.

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

        > 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

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

      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"?

        > 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.

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2020-11-23 17:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?