I think something else is going on.

Consider these three examples (note that the example string is different):

>perl -wMstrict -le "my $chr; $chr = \"x this and that y\"; $chr =~ s/(this|that)|(\w+)/$1\U$2/g; print \"$chr\n\"; " Use of uninitialized value in concatenation (.) or string ... Use of uninitialized value in concatenation (.) or string ... Use of uninitialized value in concatenation (.) or string ... X this AND that Y
and
>perl -wMstrict -le "my $chr; $chr = \"x this and that y\"; $chr =~ s/(this|that)|(\w+)/\u$1\E$2/g; print \"$chr\n\"; " Use of uninitialized value in concatenation (.) or string ... Use of uninitialized value in concatenation (.) or string ... x This and That y
and
>perl -wMstrict -le "my $chr; $chr = \"x this and that y\"; $chr =~ s/(this|that)|(\w+)/\u$1\E\U$2/g; print \"$chr\n\"; " X This AND That Y
As mentioned before, either one or the other (but not both) of the capturing groups will be satisfied, so either $1 or $2 (but not both) will be defined. In all the all examples, the regex matches five times, twice for (this|that) and three times for (\w+), but various numbers of warnings are printed.

The number of warnings depends on which capture variable is defined and which capture variable is 'transformed' by \u or \U in the replacement string. (A replacement string without any \u or \U transformations yields five warnings.)

When both $1 and $2 are transformed in some way within the replacement string, there are no warnings. Apparently, and surprisingly (to me at any rate), the \u and \U transformations in the replacement string suppress the warnings I expected.

Offhand, I can't think why this is the case.

Update: Corrected a swapped word order.


In reply to Re: why such an error happened? by AnomalousMonk
in thread why such an error happened? by lightoverhead

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.