ZetCode

Perl lcfirst Function

last modified April 4, 2025

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 as Perl passes a copy by default.

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

Modifying Variables Directly

lcfirst can modify variables in-place when used as a function.

modify.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

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

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

This shows lcfirst modifying the variable directly. 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  ";

# Trim, lowercase first, then capitalize words
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

lcfirst is useful for converting text to sentence case.

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 using lcfirst as part of the transformation process.

$ ./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.