So the RE is returning a scalar variable which is in fact what it matched and not the number of times it matched.
What you have seen here is a regex match in a list context. If there are capturing parens within the regex it will return what was captured in those parens, and if it lacks capturing parens it will return true or false (unless the /g modifier is in use then it will return what was matched). In a scalar context it merely returns whether the regex matched successfully e.g
my $str = "foo bar baz"; print "[", $str =~ /\w+ /, "]", $/; # list no capture print "[", $str =~ /\w+ /g, "]", $/; # list no capture + /g print "[", $str =~ /(\w+) /, "]", $/; # list and capture print "[". $str =~ /\w+ /, "]", $/; # scalar __output__ [1] [foo bar ] [foo] [1]
See man perlop for more info.
HTH

_________
broquaint


In reply to Re: Is this magic? by broquaint
in thread Unexepected regex match return behaviour by readey

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.