ZetCode

Perl fc Function

last modified October 8, 2026

The Perl fc function returns the case-folded version of a string. It was added in Perl 5.16 and is available with use v5.16 or later, or with use feature 'fc'.

Case folding is similar to lowercasing, but it is designed for comparisons. It follows the Unicode case-folding rules, so it can handle characters such as the German sharp s (ß) that lc does not map to a matching form.

Basic fc Usage

The simplest way to use fc is on a single string.

basic.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.16.0;

my $text = "Hello World";
my $folded = fc($text);

say "Original: $text";
say "Folded: $folded";

We apply fc to a string. The function returns a new string and leaves the original unchanged.

$ ./basic.pl
Original: Hello World
Folded: hello world

Case-Insensitive Comparison

fc enables reliable case-insensitive string comparisons. Here the German word "Straße" is compared with "STRASSE". The folded form of ß is "ss", so the two words are equal after folding.

compare.pl
#!/usr/bin/perl

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

my $str1 = "Straße";
my $str2 = "STRASSE";

if (fc($str1) eq fc($str2)) {
    say "Strings are equal when case-folded";
} else {
    say "Strings are not equal";
}

The use utf8 pragma tells Perl that the source code is UTF-8 encoded, so the literals contain the ß character. The binmode line makes STDOUT print UTF-8 correctly.

$ ./compare.pl
Strings are equal when case-folded

fc vs lc

fc provides more comprehensive case handling than lc. The following example compares both functions on a word containing ß.

comparison.pl
#!/usr/bin/perl

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

my $text = "Straße";

say "Original: $text";
say "lc: ", lc($text);
say "fc: ", fc($text);

lc keeps ß unchanged, while fc folds it to "ss". This is why fc is the better choice before comparing strings that may contain such characters.

$ ./comparison.pl
Original: Straße
lc: straße
fc: strasse

Using fc with Arrays

fc can process array elements with map.

array.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.16.0;

my @words = ("Apple", "BANANA", "Cherry");
my @folded = map { fc } @words;

say "Original: @words";
say "Folded: @folded";

We apply fc to each array element using map. Without an argument, fc works on $_. The result is a new array with case-folded versions of all strings.

$ ./array.pl
Original: Apple BANANA Cherry
Folded: apple banana cherry

Searching with fc

fc can make a case-insensitive search in Unicode text. Both the text and the search term are folded before the match.

search.pl
#!/usr/bin/perl

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

my $text = "The Greek word for wisdom is ΣΟΦΙΑ";
my $needle = "σοφια";

if (index(fc($text), fc($needle)) != -1) {
    say "Found: $needle";
} else {
    say "Not found";
}

The Greek capital letters in the text are folded to lowercase, so the lowercase search term is found.

$ ./search.pl
Found: σοφια

Case-Insensitive Hash Keys

fc helps create case-insensitive hash keys. Both the stored keys and the lookup keys are folded.

hash.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.16.0;

my %color = (fc("Red") => "#FF0000", fc("Green") => "#00FF00");

my $input = "RED";
my $key = fc($input);

say "$input is $color{$key}";

We fold the user input before using it as a key. This ensures case-insensitive access to the hash values.

$ ./hash.pl
RED is #FF0000

Performance Considerations

Case folding can be more expensive than lowercasing, especially for large texts. The following benchmark compares the two functions on the same string.

benchmark.pl
#!/usr/bin/perl

use strict;
use warnings;
use utf8;
use v5.16.0;
use Benchmark qw(cmpthese);

my $text = "ÄÖÜäöüß" x 1000;

cmpthese(-1, {
    fc => sub { my $x = fc($text) },
    lc => sub { my $x = lc($text) },
});

This benchmark compares fc with lc. The numbers depend on the machine and the Perl version, so the output below is only an example of one run. fc is usually slower, but it gives correct results for comparisons.

$ ./benchmark.pl
      Rate  lc  fc
lc 19199/s  -- -3%
fc 19883/s  4%  --

Best Practices

Source

Perl fc Documentation

This tutorial covered Perl's fc function with practical examples demonstrating its usage in Unicode string handling.

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.