in reply to s/// operator and undef question...

I can answer the first for you. m// matches the nothing at any part of the string:
my $target = 'this is a test of the servise'; $target =~ s//are/; print "$target\n";
There's a spot of nothing at the very start of the string.

I think you're missing /g at the end of the regex in the loop condition. As for that, my assumption is that there's Deep Magic here, and you shouldn't rely on these results.

Another test:

if ($target =~ /test/) { $target =~ s//quiz/; }
Maybe a blank regex condition on the left stays with what was previously matched. Dunno if that's an intended feature or not.

Update: I'm upgrading it to potential bug:

my $target = 'this is a test of the servise'; if ($target =~ /test/) { $target =~ s//test2/; } if ($target =~ /nothing/) { $target = s//null/; } else { $target =~ s//foo/; }
The moral of the story is, don't do a substitution inside a match on the same string.

Replies are listed 'Best First'.
Re: Re: s/// operator and undef question...
by merlyn (Sage) on Dec 13, 2000 at 05:41 UTC
    my $target = 'this is a test of the servise'; if ($target =~ /test/) { $target =~ s//test2/; } if ($target =~ /nothing/) { $target = s//null/; } else { $target =~ s//foo/; }
    My guess is that this returns this is a foo2 of the servise as documented in perlop:
    [....] If the pattern evaluates to the empty string, the last successfully executed regular expression is used instead. [....]
    And so my standard admonition about "don't use $1 except in the context of a conditional" would also apply here, since "most recent success" is contextual.

    -- Randal L. Schwartz, Perl hacker