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

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 $_

Replies are listed 'Best First'.
Re^5: using s/ to not only substitute but also modify text
by ikegami (Patriarch) on Nov 23, 2008 at 01:35 UTC

    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;
      Thanks very much again ikegami.