There is either a bug in perl, or in my understanding of the /n regular-expression option. (This is perl v5.24.3, in case behavior varies among releases.)
#!/usr/bin/perl $_ = 'abcdefg'; # define a string /(..)(..)(..)/; # break the string into 3 two-character chunks print "$1 $2 $3\n"; # print the chunks /(j)/n; # search for a letter that's not in the string print "$1 $2 $3\n"; # print the chunks /(g)/n; # search for a letter that's in the string print "$1 $2 $3\n"; # print the chunks
Because the latter two regular expressions use the /n option, they should have no effect on the existing values of $1, $2, and $3. And in fact the first one does not. But the second one erases all three.

I grant you, the documentation for /n is a little sketchy, saying only "Non-capture mode. Don't let () fill in $1, $2, etc..." It doesn't actually specify what happens to existing values of these variables. But I would expect consistent behavior: it should either always preserve the values (and this seems the ideal — what is ever gained from overwriting them when the user has asked that they not be populated?), or always erase them. Erasing them in the case of a successful match but not a failed one is at least unexpected, if it doesn't rise to the level of outright bug.

Or, I'm fundamentally misunderstanding something about /n. What says the wisdom of the monastery?

In reply to non-capture mode sometimes erases previous capture by raygun

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.