Polynomial has asked for the wisdom of the Perl Monks concerning the following question:
Suppose I use an s/// to alter a string but also to capture some substrings en passant. Suppose I have lots of pairs of capturing parentheses, so in order to put each substring into its own scalar I have to do something like my ($alpha, $beta, $gamma, $delta, $epsilon, $zeta) = ($1, $2, $3, $4, $5, $6);. But that seems redundant, as well as a little dangerous, since every time I add or remove a pair of capturing parentheses I have to remember not only to add or remove an lvalue but also to resize the list of rvalues.
What I'd really like to be able to do is something like my ($alpha, $beta, $gamma, $delta, $epsilon, $zeta) = @SOMEVAR;, where @SOMEVAR is one of those obscure punctuation variables. Except no such variable seems to exist. I can imagine various hacks to simulate one, like using symbolic references to collect $1 through ${@+}, and I can imagine workarounds, like using named captures. But I think there really ought to be a simple, fast, unlikely-to-break way to get all the (anonymous) captures from the last successful match as a list. Anybody know of one?
(Note that since I'm using s/// instead of m//, the return value of the pattern-matching operation isn't helpful.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Get all captured substrings after a substitution
by Fletch (Bishop) on Aug 13, 2009 at 14:34 UTC | |
by AnomalousMonk (Archbishop) on Aug 13, 2009 at 17:02 UTC | |
|
Re: Get all captured substrings after a substitution
by kennethk (Abbot) on Aug 13, 2009 at 14:34 UTC | |
|
Re: Get all captured substrings after a substitution
by ikegami (Patriarch) on Aug 13, 2009 at 14:42 UTC | |
|
Re: Get all captured substrings after a substitution
by Limbic~Region (Chancellor) on Aug 13, 2009 at 18:02 UTC | |
by Polynomial (Acolyte) on Aug 14, 2009 at 12:48 UTC |