in reply to why the need to match when assigning?
Important nit-pik: You mean parentheses, (or "parens" for short.) "Brackets" refers to []. An important difference when discussing code.
To answer the question, Perl functions can return different values depending on their context. In this case, the matching regex operator can return either a list, or a scalar. (see perlre for details). In list context, the matching regex will return the contents of parenthesized patterns. /(\d)/ puts the first digit in $1. /(\d)(\d+)/ puts the first digit in $1, and all others in $2. Be careful when using this, however, since these values will not be what you expect if the pattern doesn't match. In list context, the $1, $2, ...$N variables are returned as a list.
Thus, $count =~ /^(\d+)/ would return a boolean indicating whether a match occured. ($count) =~ /^(\d+)/ would return either undef (if no match occured) or the list ($1) (which then gets the value of $1 assigned to $count)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: why the need to match when assigning?
by davorg (Chancellor) on Jun 04, 2001 at 17:05 UTC | |
by converter (Priest) on Jun 04, 2001 at 17:33 UTC | |
by swiftone (Curate) on Jun 04, 2001 at 17:08 UTC |