Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^3: ref to read-only alias ... why? (not consistent)

by LanX (Saint)
on Jan 06, 2012 at 17:35 UTC ( [id://946633]=note: print w/replies, xml ) Need Help??


in reply to Re^2: ref to read-only alias ... why? (notabug)
in thread ref to read-only alias ... why?

Your right it's not consistent.

But IMHO consistency will most likely break legacy code.

I think that there should at least be a warning.

Since it's not always clear if a variable is an alias, this can cause very hard to detect errors in subs which are meant to modify call-by-reference parameters. (not to be confused with Perl references)

Did you check if there are already older bug-reports regarding this?

Cheers Rolf

UPDATE:

DB<108> sub inc_a { $_[0]++; return } DB<109> $x=1; inc_a($x); print $x 2 DB<110> inc_a(1) Modification of a read-only value attempted at (eval 13)[/usr/share/pe +rl/5.10/perl5db.pl:638] line 2.

but

DB<111> sub inc_b { my $r=\ $_[0]; $$r++; return } DB<112> $x=1; inc_b($x); print $x 2 DB<113> inc_b(1) DB<114>

When passing an aliasing variable instead of 1 it's the same problem. With inc_b non-aliasing vars will increment, but aliases will silently fail to increment.

Replies are listed 'Best First'.
Re^4: ref to read-only alias ... why? (not consistent)
by ikegami (Patriarch) on Jan 06, 2012 at 18:33 UTC

    Your right it's not consistent. But IMHO consistency will most likely break legacy code.

    I think you're saying that because you envision it being consistently broken (always dying) instead of it being consistently fixed (never dying).

    There are two ways of fixing it:

    • Have literal constants always return new values. That's inefficient.
    • Have literal constants return copy-on-write values. There's currently no such mechanism, and that's a lot of work to implement.

    Check out this bug that would also be fixed by either of the above two fixes:

    for (1..2) { for (1..3) { print $_++; } # 123 234 print "\n"; }
Re^4: ref to read-only alias ... why? (notabug)
by dk (Chaplain) on Jan 06, 2012 at 17:46 UTC
    Heh, it's a 8-year old (!!!!) bug: https://rt.perl.org/rt3/Public/Bug/Display.html?id=21979 . With resolution that it's indeed a bug ... well, that situation doesn't seem good to me, when noone wants to touch a bug in 8 years. I wonder why.
      Thats what I expected.

      > I wonder why.

      IMHO legacy code would break if you die here.

      But a warning would be great.

      Cheers Rolf

        I agree on a warning, it would be a practical thing, after all. And after that, a real fix with 'die' a couple versions later should help the legacy code.

        Well anyway, I found the code suggested by ikegami, so would you guys review the patch please?

        --- pp.c.0 2012-01-06 01:07:34.511988700 +0100 +++ pp.c 2012-01-06 19:04:06.311428300 +0100 @@ -515,9 +515,11 @@ SvTEMP_off(sv); SvREFCNT_inc_void_NN(sv); } - else if (SvPADTMP(sv) && !IS_PADGV(sv)) + else if (SvPADTMP(sv) && !IS_PADGV(sv)) { + Perl_ck_warner(aTHX_ packWARN(WARN_MISC), + "Implicit copy of a read-only scalar due to aliasing"); sv = newSVsv(sv); - else { + } else { SvTEMP_off(sv); SvREFCNT_inc_void_NN(sv); }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-16 18:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found