ZetCode

Perl ucfirst Function

last modified October 8, 2026 Perl 5.44

The Perl ucfirst function capitalizes the first character of a string. It's useful for formatting text to proper case or sentence case.

ucfirst only affects the first character, leaving the rest of the string unchanged. It works with both ASCII and Unicode characters.

Basic ucfirst Usage

The simplest way to use ucfirst is on a single string variable.

basic.pl
#!/usr/bin/perl

use warnings;
use v5.34.0;

my $text = "hello there";
print "Original: '$text'\n";

my $capitalized = ucfirst($text);
print "Modified: '$capitalized'\n";
print "Original remains: '$text'\n";

We demonstrate ucfirst capitalizing the first letter. The function returns a new string without modifying the original variable.

$ ./basic.pl
Original: 'hello there'
Modified: 'Hello there'
Original remains: 'hello there'

Modifying Original Variable

To modify the original string, assign the result back to the variable.

modify.pl
#!/usr/bin/perl

use warnings;
use v5.34.0;

my $name = "john doe";
print "Before: '$name'\n";

$name = ucfirst($name);
print "After: '$name'\n";

This shows how to permanently capitalize a string by reassigning the ucfirst result to the original variable.

$ ./modify.pl
Before: 'john doe'
After: 'John doe'

Working with Unicode

ucfirst properly handles Unicode characters in modern Perl.

unicode.pl
#!/usr/bin/perl

use warnings;
use v5.34.0;
use utf8;
binmode(STDOUT, ':encoding(UTF-8)');

my $greek = "γάλα"; # Greek word for "milk"
print "Original: '$greek'\n";

my $capitalized = ucfirst($greek);
print "Capitalized: '$capitalized'\n";

The example demonstrates ucfirst with a Greek Unicode string. Note the use utf8 pragma for proper Unicode handling. The binmode call makes STDOUT accept wide characters without a "Wide character" warning.

$ ./unicode.pl
Original: 'γάλα'
Capitalized: 'Γάλα'

Multiple Words

To capitalize each word in a string, combine ucfirst with split and join.

words.pl
#!/usr/bin/perl

use warnings;
use v5.34.0;

my $phrase = "perl programming language";
print "Original: '$phrase'\n";

my @words = split(' ', $phrase);
@words = map { ucfirst($_) } @words;
my $title = join(' ', @words);

print "Title Case: '$title'\n";

This script converts a string to title case by capitalizing each word's first letter. Note that split(' ') collapses runs of whitespace, so the words are rejoined with single spaces.

$ ./words.pl
Original: 'perl programming language'
Title Case: 'Perl Programming Language'

User Input Capitalization

ucfirst is useful for formatting user-provided names.

input.pl
#!/usr/bin/perl

use warnings;
use v5.34.0;

print "Enter your city: ";
my $city = <STDIN>;
chomp $city;

$city = ucfirst(lc($city));
print "Formatted city: '$city'\n";

The script reads user input, removes the newline with chomp, converts to lowercase, then capitalizes the first letter for consistent formatting.

$ ./input.pl
Enter your city: lONDON
Formatted city: 'London'

Array Processing

ucfirst can be applied to array elements using map.

array.pl
#!/usr/bin/perl

use warnings;
use v5.34.0;

my @names = ("alice", "bob", "charlie");
print "Original: @names\n";

@names = map { ucfirst($_) } @names;
print "Capitalized: @names\n";

This example processes an array of names, capitalizing each one. The map function applies ucfirst to every element.

$ ./array.pl
Original: alice bob charlie
Capitalized: Alice Bob Charlie

Combining with Other Functions

ucfirst can be combined with other string functions for powerful transformations.

combine.pl
#!/usr/bin/perl

use warnings;
use v5.34.0;

my $text = "  perl is GREAT!  ";
print "Original: '$text'\n";

# Trim, lowercase, then capitalize
$text =~ s/^\s+|\s+$//g;
$text = lc($text);
$text = ucfirst($text);

print "Processed: '$text'\n";

This script demonstrates a common text processing pipeline: trimming whitespace, converting to lowercase, then capitalizing the first letter.

$ ./combine.pl
Original: '  perl is GREAT!  '
Processed: 'Perl is great!'

Best practices

Keep these points in mind when working with the ucfirst function.

Source

Perl ucfirst Documentation

This tutorial covered Perl's ucfirst function with practical examples demonstrating its usage in common string processing scenarios.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Perl tutorials.