in reply to Parse Puzzle

Capturing parens will return a list of the matched results. In scalar context, a list returns the length of the list (Update: like merlyn and perlfunc says, in scalar context, the m// operator returns true or false). So my $ch = dis("ABC") will set $ch to "1" (true), but my ($ch) = dis("ABC") will set $ch to "C". Returning $1 in the subroutine explicitly returns the correct thing no matter the context. Some monks will argue that even with this simple regex, you should handle the case where the argument does not match the regex (e.g. the string has newlines, or does not contain even one character, etc).

Replies are listed 'Best First'.
•Re: Re: Parse Puzzle
by merlyn (Sage) on Mar 30, 2003 at 06:30 UTC
    In scalar context, a list is the length of the list.
    No. No. There's no such thing as a list in a scalar context. So there can't be a "rule" like this.

    An operator has a "scalar definition" and a "list definition". The scalar definition for regex match is a boolean indicator. See my other post.

    There are no "scalar definitions" which return a list. Period.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.