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

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.
  • Comment on Re^2: using s/ to not only substitute but also modify text

Replies are listed 'Best First'.
Re^3: using s/ to not only substitute but also modify text
by ikegami (Patriarch) on Nov 22, 2008 at 08:12 UTC
    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 $_

        the only difference is that variable being the defaulted $_ while the other is user-defined

        Nope, no "user-defined" variables being used by s/// in either snippet.

        my $r = "Time to work"; s/work/play/; print $r;

        means

        my $r = "Time to work"; $_ =~ s/work/play/; print $r;

        Since you never assigned anything to $_, you get a warning. Conversely, you never told s/// to work on $r. Doing either will solve the problem.

        my $_ = "Time to work"; s/work/play/; print $_;
        my $r = "Time to work"; $r =~ s/work/play/; print $r;