Perl lc Function
last modified November 25, 2026 Perl 5.44
The Perl lc function converts strings to lowercase. ASCII
characters are always handled as expected; for Unicode strings, lc
follows Unicode case rules.
lc works with international characters. It is locale-sensitive
only under use locale. It returns a new string rather than
modifying the original variable.
Basic lc Usage
The simplest way to use lc is on a single string variable.
#!/usr/bin/perl use warnings; use v5.34.0; my $text = "Hello There!"; my $lower = lc($text); print "Original: '$text'\n"; print "Lowercase: '$lower'\n";
We demonstrate lc converting a mixed-case string to lowercase.
The original string remains unchanged.
$ ./basic.pl Original: 'Hello There!' Lowercase: 'hello there!'
Comparing Strings Case-Insensitively
lc is useful for case-insensitive string comparisons.
#!/usr/bin/perl
use warnings;
use v5.34.0;
my $input = "YeS";
my $match = "yes";
if (lc($input) eq lc($match)) {
print "Input matches (case-insensitive)\n";
} else {
print "Input doesn't match\n";
}
This script compares user input case-insensitively by converting both strings to lowercase before comparison.
$ ./compare.pl Input matches (case-insensitive)
Processing Arrays
lc can process array elements using map.
#!/usr/bin/perl
use warnings;
use v5.34.0;
my @words = ("APPLE", "Banana", "CherRY");
my @lower_words = map { lc } @words;
print "Original: @words\n";
print "Lowercase: @lower_words\n";
We convert all elements of an array to lowercase. The original array remains unchanged while we create a new lowercase version.
$ ./array.pl Original: APPLE Banana CherRY Lowercase: apple banana cherry
International Characters
lc handles international characters correctly.
#!/usr/bin/perl use warnings; use v5.34.0; use utf8; my $text = "ÉLÉGANT Zürich"; my $lower = lc($text); binmode STDOUT, ':utf8'; print "Original: '$text'\n"; print "Lowercase: '$lower'\n";
This demonstrates lc working with accented characters and
umlauts. Note the use utf8 pragma for Unicode support.
$ ./unicode.pl Original: 'ÉLÉGANT Zürich' Lowercase: 'élégant zürich'
lc vs lcfirst
lc differs from lcfirst which only affects the
first character.
#!/usr/bin/perl use warnings; use v5.34.0; my $text = "Perl Programming"; print "lc: ", lc($text), "\n"; print "lcfirst: ", lcfirst($text), "\n";
lc converts the entire string while lcfirst only
changes the first character to lowercase.
$ ./compare_functions.pl lc: perl programming lcfirst: perl Programming
Using with Hashes
lc can normalize hash keys for case-insensitive lookups.
#!/usr/bin/perl
use warnings;
use v5.34.0;
my %colors = (
red => "#FF0000",
green => "#00FF00",
blue => "#0000FF"
);
my $user_input = "RED";
my $color_code = $colors{lc $user_input};
if (defined $color_code) {
print "Hex code for $user_input is $color_code\n";
} else {
print "Color not found\n";
}
We use lc to normalize user input before hash lookup. This
allows case-insensitive access to hash values.
$ ./hash.pl Hex code for RED is #FF0000
Locale awareness
Under use locale, lc takes the current locale into
account. The example lowercases a Slovak phrase in a Slovak, an English and
the plain C locale.
#!/usr/bin/perl
use warnings;
use v5.34.0;
use utf8;
use locale;
use POSIX qw(locale_h);
no warnings 'locale';
binmode STDOUT, ':utf8';
my $text = "ŤAŽKÝ ĽAD";
for my $loc ('sk_SK.UTF-8', 'en_US.UTF-8', 'C') {
setlocale(LC_ALL, $loc) or die "locale $loc is not installed\n";
say "$loc: ", lc $text;
}
The setlocale function from POSIX switches the
locale; it returns false when the locale is not installed. In a UTF-8 locale,
Perl applies the Unicode case rules, so the Slovak and the English locale give
the same result. Slovak has no special casing rules, and Perl does not
implement language-specific ones, such as the Turkish dotted and dotless I,
even in a UTF-8 locale.
The C locale is not UTF-8. Characters below 256, such as
Ý, then follow the locale, which knows only ASCII letters, so
Ý stays uppercase. Characters above 255, such as Ť
and Ľ, still use the Unicode rules. Perl warns about this mix;
the no warnings 'locale' line silences these warnings for the
demonstration. Without use locale, lc always uses
the Unicode rules, which is what most programs want.
$ ./locale.pl sk_SK.UTF-8: ťažký ľad en_US.UTF-8: ťažký ľad C: ťažkÝ ľad
Best practices
Keep these points in mind when working with the lc function.
- Use for normalization: Standardize strings before comparison.
- Combine with chomp: Clean input with both functions.
- Consider Unicode: Ensure proper encoding for non-ASCII.
- Performance: Avoid unnecessary conversions in loops.
Source
This tutorial covered Perl's lc function with practical
examples demonstrating its usage in various scenarios.
Author
List all Perl tutorials.