Thanks for the detailed post!

I have to admit I'm not too much concerned about aliasing in for-each-loops, because they have a limited scope.

Loosing the overview about aliases in nested for-eaches implies bad coding practice resp. the need for refactoring.

So I'm more concentrating on the @_ aliases of subs, the possible effects are not limited to the sub.

Let's suppose a sub Package_A::trim($a) which uses call-by-reference to delete leading and trailing whitespaces.

Another sub Package_B::report() might create a report, and needs to trim the entries:

sub report { trim($_) for @_; return sprintf "...",@_; }

Some code in Package_C calls report() and passes occasionally a read-only value.

Now depending on the implementation of trim() the code might

a) die with "modification of read-only ..." or

b) continue but silently fail to delete the whitespaces.

That's why I think there should be at least a warning in case b.

> I don't see a point to a switch whose sole purpose is to break working code.

I don't understand why a warning would break existing code.

In general:

And I don't see a better coding practice, except avoiding subs like trim() using side-effects by manipulation $_[]

Clearly the author of report() doesn't expect read-only arguments, but since Perl5 doesn't have signatures like in Perl6 he can't easily enforce them.

He could rely on the implementation of trim which throws an error in case a). But after the author of Package_A switches to implementation b) of trim() the functionality of Package_B is broken.

And the different test-suites of packages A,B and C do not report any problem. I hope it's understandable now why aliasing in subs bothers me more than in for-eaches.

Cheers Rolf

PS: Sorry for the delay, quite busy ATM.


In reply to Re^12: ref to read-only alias ... why? (notabug) by LanX
in thread ref to read-only alias ... why? by dk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.