Search code examples
perlcase-sensitiveuppercase

Determine if a string starts with an upper-case character in Perl


I need to make a subroutine in Perl which determines if a string starts with an upper-case character. What I have so far is the following:

sub checkCase {
    if ($_[0] =~ /^[A-Z]/) {
        return 1;
    }
    else {
        return 0;
    }
}

$result1 = $checkCase("Hello");
$result2 = $checkCase("world");

Solution

  • That's almost correct. But [A-Z] might not match uppercase accented characters depending on your locale. Better to use /^[[:upper:]]/.

    Also your subroutine invocations shouldn't have the $ character in front of them. I.e. they should be:

    $result1 = checkCase("Hello");
    $result2 = checkCase("world");