Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^8: Shouldn't references be readonly? (updated)

by ikegami (Pope)
on Aug 09, 2020 at 06:55 UTC ( #11120513=note: print w/replies, xml ) Need Help??


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

Making the var read-only won't prevent concatenation or addition, so it doesn't matter that $x . [] or $x + [] doesn't make sense.[1]. This isn't relevant to the topic at hand.

The only thing making it read-only will do is prevent assignment, and map { $_ = 3 } $x.$y makes no more sense than map { $_ = 3 } [].

Either prevent both of them, or prevent neither of them. No need for baseless inconsistencies.


  1. That said, $ref+0 makes some sense for refs to things other than objects with overloads, and both concatenation and addition make sense for refs to objects with overloads (which aren't farfetched at all!)

Replies are listed 'Best First'.
Re^9: Shouldn't references be readonly? (updated)
by ikegami (Pope) on Aug 09, 2020 at 07:12 UTC

    Note that assigning to certain "unassigned scalars" is legitimate, such as the one returned by substr. Unassigned scalars with set magic simply wouldn't be made read-only, of course.

    But you know what else wouldn't be made read-only? "Unassigned scalars" from XS code. Making unassigned scalar *created by Perl* read-only would create inconsistencies with those created by XS code. This reduces the benefits of making any change.

Re^9: Shouldn't references be readonly? (updated)
by LanX (Sage) on Aug 09, 2020 at 12:46 UTC
    My point is about potential problems in backwards compatibility.

    A transformation of a string or a number into another string or number is common, BUT the transformation of refs is impossible.

    Hence it's far more likely that legacy code will break if something like $_++ became illegal for an aliased input like 3+4 .

    Same code for refs wouldn't make sense at all.

    I can't come up with more intuitive examples, but I'm sure those exist.

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

      Put differently, you want to prevent transformation of references, but using a completely ineffective tool to achieve it. Making references read-only doesn't prevent transformation.

      Preventing transformation (stringification, numification, but not booleanification) of references might be a good idea.

      Issues:

      • Intentional uses of $ref+0 would have to be replaced by refaddr($ref).
      • Intentional uses of "$ref" would have to be replaced.
      • Programs that mostly function will start dying instead.

      Benefits:

      • Catch some bad code (But what code wouldn't already be obviously wrong?)

        See also stringification, which allows you to say:

        no stringification;

        and forbids

        "$ref" # stringify $ref . "foo" # concat lc $ref lcfirst $ref uc $ref ucfirst $ref quotemeta $ref

      Since making the scalar read-only doesn't prevent transformation (only assignment), there is no backwards compatibility issue relating to transformation.

      Hence it's far more likely that legacy code will break if something like $_++ became illegal for an aliased input like 3+4 .

      And that's exactly what the change is suppose to prevent. What's the point of making the change if it doesn't prevent exactly what it's suppose to be preventing!

      If you say there's no point in preventing $_ = ... when $_ is an unassigned string, then there's no point in preventing it when it's an unassigned reference.

        I'm giving up.

        I once vowed to avoid diving deeper than Re^10 and here we are! :)

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2021-11-27 18:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?