Perl uc function (which stands for uppercase) is meant to return a uppercase version of a string expression. Some useful examples of how to use this function in your scripts will be given to you in this tutorial. Click the lc link to see the correspondent lc Perl function which converts a string expression to lowercase characters.There are two syntax forms for this function:
Please note that the Perl uc function doesn’t alter the string variable content, but it returns an uppercase copy of the string. If you omit the EXPR parameter, the Perl uc function will use the content of the special variable $_.Here is an example about how to use it:
# initialize some string variable
$string = "This is about Perl Functions";
uc($string);
print $string, "\n";
# displays: This is about Perl Functions
$string = uc($string);
print $string, "\n";
# displays: THIS IS ABOUT PERL FUNCTIONS
If we don’t make an assignment for the variable we convert through the uc function, the result of the conversion will be lost – you can see this in the 3-5 lines of the above code. At the end of the code we store the uppercase converted value in the same variable (line 6) and we print it again.This uc function is very useful when you perform some string comparisons. Try to remember that all comparisons are case sensitive and the following strings "uc" and "UC" are not the same.
Now let’s consider the case insensitive comparison of two strings. We can use the uc function either converting both strings in lowercases using an assignment operator and than make the comparisons or compare the strings by calling the uc function for each of them, without changing their values.
The next short example shows you how to do this:
$string1 = "blue";
$string2 = "Blue";
$string1 = uc($str1);
$string2 = uc($str2);
if($string1 eq $string2)
{
print "The two strings are equal\n";
}
#comparison without altering the string values
$string1 = "blue";
$string2 = "Blue";
if(uc($string1) eq uc($string2))
{
print "The two strings are equal\n";
}
NEW!!!
Do you want more information about the basic Perl topics?
Check my new "Perl How To" Tutorial eBooks page where I'll answer the most frequent questions regarding some topics :