How to capitalize first letter of each word in Perl?

Question

How to capitalize first letter of each word in Perl?

Capitalizing the first letter of each word in a string is a common text-processing task in Perl. This is often referred to as "title case" or "capitalizing words." In Perl, you can achieve this by using regular expressions combined with the \U escape to uppercase characters.

Basic Approach Using Regular Expressions

The simplest way to capitalize the first letter of each word is to use the substitution operator s/// with a pattern that matches word boundaries and then uppercases the first character.

  • \b matches a word boundary.
  • (\w) captures the first letter of the word.
  • \U in the replacement part uppercases the following character.

For example:

my $string = "hello world from perl";
$string =~ s/\b(\w)/\U$1/g;
print "$string\n";    # prints "Hello World From Perl"

Explanation of Perl Concepts

  • \b: Word boundary anchor, useful to find word starts.
  • (\w): Capturing group for the first letter of the word.
  • \U: Uppercase the following character or characters.
  • $1: Refers to the first captured group inside the replacement string.
  • The g modifier: Applies the substitution globally (to all matches in the string).

This approach works well for ASCII letters. If your input contains Unicode characters, consider using the /u modifier (Perl 5.14+) for Unicode-aware matching:

$string =~ s/\b(\w)/\U$1/gou;

Common Pitfalls

  • Lowercase the rest: This method only uppercases the first letter but doesn't lowercase the rest of the word. If your input is mixed case and you want strictly title case, you might want to lowercase the entire string first, then apply the uppercase on first letters:
my $string = "heLLo woRLd";
$string = lc $string;           # lowercase all first
$string =~ s/\b(\w)/\U$1/g;    # capitalize first letter
print "$string\n";              # prints "Hello World"
  • Non-word characters: This technique capitalizes letters after apostrophes and digits consistently, so for cases like "O'reilly" it will uppercase the O but not the r.
  • Underscores or special characters: Since \w includes underscores, those will be treated as part of words.

Complete Runnable Example

#!/usr/bin/perl
use strict;
use warnings;

my $text = "the quick brown fox jumps over the lazy dog. o'reilly's book.";
print "Original: $text\n";

# Lowercase everything to normalize
$text = lc $text;

# Capitalize first letter of each word
$text =~ s/\b(\w)/\U$1/g;

print "Title Case: $text\n";

This prints:

Original: the quick brown fox jumps over the lazy dog. o'reilly's book.
Title Case: The Quick Brown Fox Jumps Over The Lazy Dog. O'Reilly's Book.

Summary

In Perl, capitalizing the first letter of each word can be done easily with a substitution using \b(\w) and the \U escape. Remember to normalize the case first if you want consistent title casing. This approach highlights Perl’s flexibility and “There's More Than One Way To Do It” (TMTOWTDI) philosophy in string manipulation.

Verified Code

Executed in a sandbox to capture real output. • v5.34.1 • 7ms

Tip: edit code and use “Run (Browser)”. Server runs always execute the published, verified snippet.
STDOUT
Hello World From Perl
STDERR
(empty)

Was this helpful?

Related Questions