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).