ZetCode

Perl lcfirst Function

last modified October 8, 2026

The Perl lcfirst function converts the first character of a string to lowercase. It's useful for formatting text and normalizing string inputs.

lcfirst leaves the rest of the string unchanged. It returns the modified string or the original if the first character is already lowercase.

Basic lcfirst Usage

The simplest way to use lcfirst is on a single string value.

basic.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

my $text = "Hello World";
my $result = lcfirst($text);

print "Original: '$text'\n";
print "Modified: '$result'\n";

We demonstrate lcfirst converting the first 'H' to lowercase. The original string remains unchanged, because lcfirst returns a new string.

$ ./basic.pl
Original: 'Hello World'
Modified: 'hello World'

Updating Variables

lcfirst does not modify its argument. It returns a new string, so assign the result back to the variable to change it.

modify.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

my $name = "JOHN";
print "Before: $name\n";

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

This assigns the result of lcfirst back to the variable. The first character is lowercased while others remain unchanged.

$ ./modify.pl
Before: JOHN
After: jOHN

Working with Mixed Case Strings

lcfirst only affects the first character, regardless of case.

mixed.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

my @words = ("Perl", "python", "Ruby", "java");

foreach my $lang (@words) {
    print lcfirst($lang), "\n";
}

The example processes programming language names. Only first letters are lowercased, even if other uppercase letters exist in the string.

$ ./mixed.pl
perl
python
ruby
java

Chaining with Other Functions

lcfirst can be combined with other string functions for complex transformations.

chain.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

my $text = "  THE QUICK BROWN FOX  ";

# split(' ') trims whitespace; title-case each word, join, then lowercase the first character
my $result = lcfirst(join(' ', map { ucfirst lc } split(' ', $text)));

print "Result: '$result'\n";

This demonstrates combining lcfirst with string manipulation functions to create a title case string with lowercase first word.

$ ./chain.pl
Result: 'the Quick Brown Fox'

Handling Empty Strings

lcfirst handles edge cases like empty strings gracefully.

empty.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

my $empty = "";
my $space = " ";
my $single = "A";

print "Empty: '", lcfirst($empty), "'\n";
print "Space: '", lcfirst($space), "'\n";
print "Single: '", lcfirst($single), "'\n";

The example shows lcfirst behavior with edge cases. Empty strings and strings with only whitespace remain unchanged.

$ ./empty.pl
Empty: ''
Space: ' '
Single: 'a'

Using with Unicode Characters

lcfirst works correctly with Unicode characters when properly configured.

unicode.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;
use utf8;

binmode STDOUT, ':utf8';

my $greek = "ΈΛΛΑΣ"; # Greece in Greek
my $russian = "Россия"; # Russia in Russian

print lcfirst($greek), "\n";
print lcfirst($russian), "\n";

This demonstrates lcfirst with Unicode strings. The function correctly handles non-ASCII first characters when UTF-8 is enabled.

$ ./unicode.pl
έΛΛΑΣ
россия

Creating Sentence Case

Sentence case capitalizes the first letter of each sentence and lowercases the rest. This example builds it with lc and ucfirst.

sentence.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

sub to_sentence_case {
    my ($text) = @_;
    $text = lc($text);
    return ucfirst($text);
}

my $paragraph = "THIS IS A SAMPLE PARAGRAPH. IT HAS MULTIPLE SENTENCES.";

# Split into sentences and process each
my @sentences = split(/(?<=[.!?])\s+/, $paragraph);
@sentences = map { to_sentence_case($_) } @sentences;

print join(' ', @sentences), "\n";

The example converts all-caps text to sentence case by lowercasing each sentence with lc and capitalizing its first letter with ucfirst.

$ ./sentence.pl
This is a sample paragraph. It has multiple sentences.

Best Practices

Source

Perl lcfirst Documentation

This tutorial covered Perl's lcfirst function with practical examples demonstrating its usage in various scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Perl tutorials.