if ($string =~ m!(^[A-Za-z0-9]{4})$!) { $string =~ tr/[a-z]/[A-Z]/; # or $string = uc $string; # do something } else { # do some error }