in reply to Re: /g option not making s// find all matches
in thread /g option not making s// find all matches

... keeps the whole thing inside a single s/// operator.

The only thing I would say about this is that you're firing up the eval-uator behind the scenes, so
    s/^ ~ .* /@{[  ${^MATCH} =~ tr#_#+#r  ]}/xp;
is (I think; haven't tested it) exactly equivalent to
    s/^ ~ .* / ${^MATCH} =~ tr#_#+#r /xpe;
That's an awful lot of moving parts for what seems a fairly simple match and transformation. Getting back to Eily's first solution and in particular to tybalt89's solution, I don't see a problem with something like (also untested):

while (<FILEHANDLE>) { tr/_/+/ if m{ \A ~ }xms; do_something_with_fixed_up_line($_); }
Simple, clear, one-step fixup, do whatever you want with the line thereafter.

Oh, and BTW: Please don't use # as a delimiter for regex expressions; I know we're talking TimToady here, but there's no need for perversity!


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^3: /g option not making s// find all matches
by raygun (Scribe) on Jun 01, 2018 at 04:11 UTC
    Simple, clear, one-step fixup
    Yes, and absolutely the solution I would use if my real-life problem matched this simplified example of looping through an array or input stream. My code is doing the opposite: siccing an array of regex substitutions on a single string. Thus each modification has to be containable in an s///. (I can handle special cases outside this array, but minimizing special cases is a goal.)
    Please don't use # as a delimiter for regex expressions
    Yeah, I get that it indicates a comment, but for simple, inline expressions such as that one (a whole six characters after the tr), I like it for how it visually stands out better than most characters within a typical regular expression, making it easy to find the boundaries of each element. In code I write, all comments are set off with plenty of whitespace, so the eye won't be tricked into thinking a comment is lurking in the middle of what otherwise looks like a line of code. ("@" also stands out in many terminal fonts, but that would be true perversity.)