in reply to why the need to match when assigning?

1. why is there a need to put brackets around "$count" ?

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
    Important nit-pik: You mean parentheses, (or "parens" for short.) "Brackets" refers to []. An important difference when discussing code.

    This was something that came as a complete surprise to me when 'Merkans started reviewing early drafts of DMP. In the UK, the word brackets denotes (), [] are 'square brackets', {} are 'curly brackets' (or 'braces') and <> are 'angle brackets'. 'Parentheses' isn't in common use at all.

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

      I suppose this would explain the use of term bracket variable when referring to $1 and friends?

      Ack! Well that's good to know.