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


In reply to Re: why the need to match when assigning? by iakobski
in thread why the need to match when assigning? by slok

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.