in reply to Re: Using eval to s/// with a pattern supplied on the command-line
in thread Using eval to s/// with a pattern supplied on the command-line

Yes, but this code
for (qw(your hand is too cold to hold)) { s/cold/new/; print $_, "\n"; }
should run and DWIM. I have no idea why it doesn't. I get Modification of a read-only value attempted at C:\t.pl line 3.


holli, /regexed monk/

Replies are listed 'Best First'.
Re^3: Using eval to s/// with a pattern supplied on the command-line
by betterworld (Curate) on Mar 16, 2005 at 22:08 UTC
    This is because the qw-list is a literal, thus not modifyable.
Re^3: Using eval to s/// with a pattern supplied on the command-line
by Limbic~Region (Chancellor) on Mar 16, 2005 at 23:28 UTC
    holli,
    Would you expect "cold" =~ s/cold/new/; to work? Remember that the looping variable in a Perl style for loop is an alias to that which is being looped over.

    Cheers - L~R

      No, I don't. But the regex operates on $_ and therefore I would have expected to be changeable.


      holli, /regexed monk/
        holli,
        But as I said, $_ isn't a copy it is an alias. If you were to do the following:
        my @word = qw(one two three four five); for ( @word ) { $_ = uc( $_ ); } print "$_\n" for @word;
        You find the underlying array being modified. If the thing being looped over can't be modified you can't change the looping variable. You need to make an explicit copy if you want to do that.

        Cheers - L~R