Perl ucfirst Function

The ucfirst function converts the first character of a string in uppercase and returns the new string.

The ucfirst function takes a string, converts its first character in uppercase and than returns the new string.

The syntax forms of the ucfirst function are as follows:

ucfirst EXPR
ucfirst
 
It returns the value of EXPR with its first character in uppercase. If the argument is a string variable, this function returns the variable with its first character in uppercase, but it doesn’t alter the content of the variable.

In the second syntax form, EXPR is omitted, so the Perl ucfirst function uses the special variable $_.

As with other functions, you can use or omit the parentheses, as you like. If the first letter of the string expression is uppercase, the function will do nothing.

The following simple example shows you how to convert the first character of a string in uppercase, using the ucfirst function:

#!/usr/local/bin/perl
 
use strict;
use warnings;
 
# initialize a string variable
my $str =  "an example about ucfirst function";
my $newStr = ucfirst $str;
 
print "$newStr,\n";
# it prints: An example about ucfirst function

The following examples show you how to capitalize all words in a string. The first example uses the ucfirst function.

See the code:

#!/usr/local/bin/perl
 
use strict;
use warnings;
 
# initialize a string variable
 
my $str =  "an eXample, about ucFirst funcTion";
 
$str = join ' ', map ucfirst, split /\W+/, lc $str;
 
print "$str\n";
 
# it prints: An Example About Ucfirst Function
This code does the following:
 
  • the $str string variable is initialized with a text
  • the next one-line statement can be explained from right to left:
    • the lc function returns an lowercase version of the string
    • the split function will split the string returned by lc into an array of words, using the \W+ delimiter (\W is a special character and means a  non-word character – anything other than letter, number or underscore; + is a quantifier and means to match one or more characters); you can replace the delimiter \W+ with whatever characters you want
    • the map function assigns in turn to $_ each element of the array returned by split and converts its first character in uppercase by using the ucfirst function (ucfirst has no argument so it acts against $_); map will return the new array  
    • the join function will turn the array returned by map into a string, using the space character as a delimiter;
    • finally, the string returned by join will be assigned to $str
  • the $str string variable is printed

If you are a bit confused about this one-line statement, I am ready to jump in your help so I’ll show you the parentheses version of this code line:

$str = join (' ', map (ucfirst, split (/\W+/, lc ($str))));
Well, to be sincere, I liked it better before wrapping it with parentheses.

Another approach is to use the s/// substitution operator of the regular expression engine, as you can see in the below example:

#!/usr/local/bin/perl
 
use strict;
use warnings;
 
# initialize a string variable
my $str = "an example, about ucfirst function";
 
$str =~ s/\b(\w)/uc($1)/eg;
 
print "$str\n";
# it prints: An Example, About Ucfirst Function
Here the s/// substitution operator is used with two modifiers:
 
  • /e  that let us evaluate right side as code (the uc function)
  • /g  allows substitute all matching text, instead of the first occurrence

The \b is a word boundary special character and matches only at the beginning of a group of word characters; the \w is the special character for a word character (letter, number or underscore). The Perl uc function acts against the $1 special variable that stores the first character of a word.