slok has asked for the wisdom of the Perl Monks concerning the following question:

I need some help in explaining the following code:

my $count=10; if(@ARGV && $ARGV[0] =~ /'^-') { ($count) = $ARGV[0] =~ /^(\d+)/; shift(@ARGV); }
questions will be wrt the line
===
($count) = $ARGV[0] =~ /^(\d+)/;
===
1. why is there a need to put brackets around "$count" ?
2. how did $count get the "digit value"?
in the pattern matching, it only says check to see if ARGV[0] begins with a "-" and that it is a digit after that.
it doesn't seems to do a substring of any sort?

Replies are listed 'Best First'.
Re: why the need to match when assigning?
by davorg (Chancellor) on Jun 04, 2001 at 12:58 UTC

    The line means "match this regular expression against $ARGV[0] and assign any captured values to ($count)".

    The regular expression looks for one or more digits at the start of the string. The \d+ is in parenthesis so the digits are captured into the special variable $1.

    In scalar context the match operator returns true or false depending on whether the match succeeds or not. In list context, it returns the values of the match variables $1, $2, etc. The parenthesis around $count forces the assignment into list context. The nett result is that the set of digits that were matched at the start of $ARGV[0] ends up in $count.

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

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

Re: why the need to match when assigning?
by iakobski (Pilgrim) on Jun 04, 2001 at 13:16 UTC
    The pattern match operator returns a list - you can say
    @list = $string =~ /(regex1)(regex2)/;
    you can also say
    ( $match1, $match2 ) = $string =~ /(regex1)(regex2)/;
    which is why you have the parentheses. You just have one item in the list in your example.

    If you omit the parentheses you just get the number of matches, ie the list is forced to scalar context. In your example you get 0 or 1.

    This is useful because you can say

    if ( $string =~ /(regex1)/ ){ $found = $1; }
    As for question 2, I can't see how it does. There is a typo in your line:
    if(@ARGV && $ARGV[0] =~ /'^-') {
    If as you say this is to check that it starts with a '-', then I would guess that you mean:
    if(@ARGV && $ARGV[0] =~ m'^-') {
    Then the if block would only be entered if $ARGV[0] starts with "-". In the block, $count only gets a value if $ARGV[0] starts with one or more digits. Which it can't because it starts with a "-"!

    -- iakobski

Re: why the need to match when assigning?
by swiftone (Curate) on Jun 04, 2001 at 16:52 UTC
    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)

      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.