in reply to using s/ to not only substitute but also modify text

Hi monks, I tried the suggested code and got an Error stating "Use of uninitialized value $_ in substitution s/// at..." Seems only the default $_ yields no errors, if any other user-defined variable (eg. $testvar) is used, the error shows up. Can anyone tell me why this is? Thanks.
  • Comment on Re: using s/ to not only substitute but also modify text

Replies are listed 'Best First'.
Re^2: using s/ to not only substitute but also modify text
by Anonymous Monk on Nov 22, 2008 at 07:47 UTC
    Sorry, the Error I mentioned came from another test I did after I got the initial 'error' from using the posted code. Basically, when I ran the code, it worked but instead of getting the hard-coded substituted text "rab", I got "function()". Then I edited the code and used an arbitrary variable name like $testvar, this is when I got the Error telling me about the uninitialized value. Thanks in advance for any suggestions anyone can tell me.
      s/.../.../;

      means

      $_ =~ s/.../.../;

      The warning is issued when $_ contains undef.

        Thanks ikegami. But I still don't understand why I get the error from a script containing the same logic of another, the only difference is that variable being the defaulted $_ while the other is user-defined Eg: my $r Below are the scripts I tested... I'd like to assign a string or even a large body of text to a variable other than $_ Can you help? Thanks.

        my $r = "Time to work"; s/work/play/; print $r; #end of script 1 #script 2 $_ = "Time to work"; s/work/play/; print $_