http://qs321.pair.com?node_id=11120332


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

> 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

  • Comment on Re^4: Shouldn't references be readonly?

Replies are listed 'Best First'.
Re^5: Shouldn't references be readonly?
by dave_the_m (Monsignor) on Aug 05, 2020 at 18:32 UTC
    Having had a further look at your OP, you seem to be complaining that the scalar ref value should readonly - I was thinking that you were wanting the anonymous array to readonly?

    So is the following a correct summary of your position as to how you would like things to work?

    [ 1, 2, 3]; # a readonly temporary reference to a mutable anonymous a +rray [ 1, 2, $x]; # also a readonly temporary reference to a mutable anonym +ous array bless 'Foo', [1,2,3];# a readonly temporary reference to a mutable ano +nymous array # where the array was mutated by blessing it $a = [1, 2, 3]; # $a is a rw copy of the temp ro array reference

    Dave.

      My complaint is primarily about aliases, where I don't understand why Perl complains about when overwriting an alias to 1 but not one to [ 1,2,3,$x] (see examples in my OP especially the update)

      Both are literal constructs unbound to any named variable.

      I'm NOT saying that anonymous arrays -ie the content - should be immutable, but their reference.

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

      > So is the following a correct summary of your position as to how you would like things to work?

      after rereading it, yes this follows my mental model.

      Analog to

      1; # a readonly temporary scalar $a = 1 ; # $a is a rw copy of the temp scalar

      Of course I could be wrong, but I'm still waiting for someone to understand my question...

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

        I'm still waiting for someone to understand my question...

        I understand it (I think) :-) The difference between map { $_++ } 1 failing and map { $_++ } [] not failing, or even map { $_++ } \1 not failing, is pretty clear to me. I just don't have an answer for you :-(

        With [], I understand the arguments that it's a constructor, for example sub foo () { 1 } can be inlined, but sub bar () { [] } returns a new anonymous array each time. But something like map { $_++ } \undef or map { $_=3 } \1 not failing doesn't entirely make sense to me yet (note map { $$_++ } \undef does fail).

        As an aside, I was playing around in the console a bit and can't yet wrap my head around this, it feels buggy, but that may just be because it's getting late here:

        $ perl -wMstrict -MData::Dump -e 'dd $_ for map { $_++ } \undef' \undef $ perl -wMstrict -MData::Dump -e 'dd $_ for map { ++$_ } \undef' 94067726450329

        Update: Nevermind, it clicked, d'oh. Also made a few minor edits to the above. Update 2: Oops, looks like my update came at just about the same time as your reply, sorry.