in reply to Bug or feature? s/// and the g option

I'm curious what the RE gurus looking into the monastery will say. However, so far, it seems to me to be neither a bug nor a feature, but just an oddity that comes from an somewhat unfortunate [mis]use of /g.

As I understand it, /g is not a substitute for /sm - it is sort of an iterator that lets you steadily step through the matches in a string, if you need a step-by-step, match-by-match approach. See it in conjunction with pos.

Successive, iterated substitution - which /g seem to imply, are clearly weird: the intermediate string resulting after every substitution is something different from both the initial string as from the final result, and there is by no means any intent to use it for anything other then as 'something' unfinished.

Think about something like
my $test = 'AAAA'; my $x1 = $1 if $test =~ s/A/AA/g; my $x2 = $1 if $test =~ s/A/AA/g; ...
Would you expect any intermediate results ?! I think not, and so I wouldn't expect anything from $1,$2,... after a s//g, similar to like I don't really trust for example a for-loop control variable to be something I can rely on once the loop has finished. I remain curious about what others think/know about it.

Replies are listed 'Best First'.
Re^2: Bug or feature? s/// and the g option
by ikegami (Patriarch) on Oct 14, 2007 at 18:49 UTC

    As I understand it, /g is not a substitute for /sm

    print is not a substitute for system. True, but obvious.

    In fact, not only are they not equivalent, s, m and g are orthgonal.
    s affects what . matches.
    m affects what ^ and $ matches.
    g affects the number of substitutions that will be made.

    /g is sort of an iterator that lets you steadily step the matches in a string

    No. You're thinking of m//g in void and scalar context. That's neither the case for m//g in list context nor for s///g.

    my $x1 = $1 if $test =~ s/A/AA/g;

    Off-topic, but my $var if ... is wrong. my has a run-time effect, so it shouldn't be executed conditionally.

      Thank you, ikegami - all objections gratefully accepted.

      Turning back to the original question: so _should_ $1 contain something you could normally rely upon after a successfull s///g ?