Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-20 04:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found