Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Problems with $_

by SilasTheMonk (Chaplain)
on May 26, 2011 at 07:54 UTC ( #906762=perlquestion: print w/replies, xml ) Need Help??
SilasTheMonk has asked for the wisdom of the Perl Monks concerning the following question:

I am having problems with the most recent version of Test::Spelling. One tends now to get the following error: Modification of a read-only value attempted at /usr/share/perl5/Test/ line 223, <DATA> line 1002. Now I am fairly sure I understand what is going on here. In add_stopwords, the 0.11 version had:
for (@_) { my $word = $_; $word =~ s/^#?\s*//;
The latest version has
for my $word (@_) { $word =~ s/^#?\s*//;
So in the latest version we are modifying an element of the array of stopwords (which will tend to be an array of constants). In the old version the element was copied so there was no problem.

Now what is the real problem here? I thought the distinction between using $_ directly and copying it is a bit obscure, especially for non-critical code. I submitted a patch that does a non-modifying extraction via regex: rt://68471 but the module author simply proposes reverting. There is a comment in the code promising something more modern - so why not now. Also why does the code need to strip off spaces - surely that will happen anyway? Can someone explain why the problem goes away when I try to debug it?

Replies are listed 'Best First'.
Re: Problems with $_
by jethro (Monsignor) on May 26, 2011 at 09:33 UTC

    What's to stop someone else to revert your patch because he thinks he might remove an unneccessary 'if' clause?

    The "normal" solution here would be to change the subroutine to the generic

    sub add_stopwords { my @words= @_; for my $word (@words) {

    Not that it is any better than the other two solutions but people might know better than to optimize that away without thinking about the consequences

    To make sure nobody changes it back a comment could be added. And/or a test for it could be added (best practice for any bug found)

    If you want to prod the unpaid, does-it-out-of-the-kindness-of-his-heart author of the module to modernize the code and add tests there are better ways than saying "There is a comment in the code promising something more modern. Is this not a great time to deliver?". I may be wrong, but to me it sounds more like a sugar-coated reproach than an appeal to his kindness.

      I may be wrong, but to me it sounds more like a sugar-coated reproach than an appeal to his kindness.
      So right. I read a very interesting book by Steven Pinker recently called the "The Stuff of thought." The chapter on indirect speach and politeness was a major revelation. I suddenly realized how I had been offending people all my life. I am still struggling to change the habits however.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://906762]
Approved by davies
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2017-08-18 22:25 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (310 votes). Check out past polls.