That has nothing to do with regexes (and that's why you didn't find it in perlre) but everything with context of the assignment.

Perl has 2 main contexts: scalar context, and list context. A function may behave differently depending on the context. (Actually there are 3 contexts: void context is the third, but it's often treated as a special case of scalar context.) For example: and array returns the array items in list context, and the number of items in scalar context. Example:

@array = ('a', 'b', 'c'); $x = @array; # scalar context => 3 @y = @array; # list context => ('a', 'b', 'c') ($v, $w) = @array; # list context so $v => 'a', $w => 'b'

When you put parentheses around the assignees on the left of the assignment, you get list context. The result is flattened to a list (individual items) and the items on the left get assigned the value of the item at their own position in the list. If there are too few items, the rest gets assigned undef; if there are too many, the remainder is ignored.

And that is what's happening here: the regex is called in list context so it returns the captured items (the value for the patterns in parens) and from that list the value of $1 is assigned to $substring.

Official docs: "Context" in perldata — see also wantarray for making your own functions behave differently depending on context; and scalar to force scalar context on a function call.

For regexes, the docs on context are in perlop (because the // is considered a kind of quotes, and quotes are treated as operators.)


In reply to Re^3: Substring consisting of all the characters until "character X"? by bart
in thread Substring consisting of all the characters until "character X"? by TheMartianGeek

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.