2 ms·
I'm guessing that it's a compound assignment. /^(.)/ is a regex match with a capture group. Perhaps this produces a list or tuple like ("a") if the buffer star
by kazinator 4y ago
I'm guessing that it's a compound assignment.
/^(.)/ is a regex match with a capture group. Perhaps this produces a list or tuple like ("a") if the buffer starts with "a", and that becomes the value of $fc.
So that is to say, maybe ($x, $y, $z) = /(\d+)-(\d+)-(\d+)/ breaks down a string like 123-334-559 such that $x gets "123", $b gets "334" and $z gets "559".
I did a very small amount of Perl 4 programming in the mid-to-late 1990s.
- dale_glass 4y agoNot that, no. ($fc) = /^(.)/ We're doing a regexp match, capturing the first character of a string. This is an usage of the implicit $_ magic variable. The () in the regexp means we want to capture this part, and the "." matches any character. ^ is the start of the string. We're doing ($fc) rather than $fc because regular expression matches work differently depending on context. In array context they return a list of matches. In scalar context they return a match count. So the ($fc) forces an array context to return the actual match. So, TL;DR in a more traditional language this would be: firstChar = left(someString, 1) And actually in Perl you could do: $fc = substr($var, 0, 1); as well, which would be far more understandable to a less Perl-inclined person, but with regexes being such a core part of the language they tend to show up for purposes users of other languages may not expect. This is indeed a pretty good demonstration of some of Perl's quirkiness. If you like it, it's nice and flexible, if you don't, then it's annoying as hell.