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

You don't check whether the eval succeeds or not. In fact, it is failing, as you can see by printing $@:
my $op = shift; for (qw/your hand is too cold to hold/) { eval $op; print $@, $_, $/; } __OUTPUT__ Modification of a read-only value attempted at (eval 1) line 1. your Modification of a read-only value attempted at (eval 2) line 1. hand Modification of a read-only value attempted at (eval 3) line 1. is Modification of a read-only value attempted at (eval 4) line 1. too Modification of a read-only value attempted at (eval 5) line 1. cold Modification of a read-only value attempted at (eval 6) line 1. to Modification of a read-only value attempted at (eval 7) line 1. hold
One way to fix it is to change the for loop to for my $x (...) and add local $_ = $x at the top of the loop.

blokhead

Replies are listed 'Best First'.
Re^2: Using eval to s/// with a pattern supplied on the command-line
by ChrisS (Monk) on Mar 16, 2005 at 21:54 UTC
    Well, I was right, forgetting to check $@ is a pretty dumb mistake.

    Thanks for the catch.

Re^2: Using eval to s/// with a pattern supplied on the command-line
by holli (Abbot) on Mar 16, 2005 at 21:53 UTC
    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/
      This is because the qw-list is a literal, thus not modifyable.
      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/