Perl uc Function
last modified October 8, 2026
The Perl uc function converts all characters in a string to
uppercase. It's a built-in string function. For character strings, it applies
Unicode case rules.
uc returns a new string and doesn't modify the original. To
change a variable itself, assign the result back to it.
Basic uc Usage
The simplest way to use uc is on a single string variable.
#!/usr/bin/perl use strict; use warnings; use v5.34.0; my $text = "Hello World"; my $upper = uc($text); print "Original: $text\n"; print "Uppercase: $upper\n";
We demonstrate uc converting a string to uppercase. The original
string remains unchanged while a new uppercase version is created.
$ ./basic.pl Original: Hello World Uppercase: HELLO WORLD
In-Place Conversion
To modify a string directly, combine uc with assignment.
#!/usr/bin/perl use strict; use warnings; use v5.34.0; my $text = "perl programming"; print "Before: $text\n"; $text = uc($text); print "After: $text\n";
This shows how to convert a string to uppercase while modifying the original variable. The assignment replaces the original value.
$ ./inplace.pl Before: perl programming After: PERL PROGRAMMING
Locale Awareness
With use locale, uc respects the current locale for byte strings.
Character strings always follow Unicode rules.
#!/usr/bin/perl use strict; use warnings; use v5.34.0; use utf8; use locale; binmode STDOUT, ':encoding(UTF-8)'; my $text = "straße"; # German street my $upper = uc($text); print "Original: $text\n"; print "Uppercase: $upper\n";
With a decoded (use utf8) string, uc follows Unicode rules,
and the German ß becomes SS whatever the locale. Byte strings, by contrast,
depend on the system locale.
$ ./locale.pl Original: straße Uppercase: STRASSE
Array Processing
uc can process array elements with map.
#!/usr/bin/perl
use strict;
use warnings;
use v5.34.0;
my @words = ("apple", "banana", "cherry");
my @upper = map { uc } @words;
print "Original: @words\n";
print "Uppercase: @upper\n";
We use map to apply uc to each array element.
This creates a new array with all elements converted to uppercase.
$ ./array.pl Original: apple banana cherry Uppercase: APPLE BANANA CHERRY
Hash Keys Conversion
uc is useful for normalizing hash keys to uppercase.
#!/usr/bin/perl
use strict;
use warnings;
use v5.34.0;
my %config = (
"timeout" => 30,
"retries" => 3,
"debug" => 0
);
my $key = "Timeout";
print "Value: ", $config{lc $key}, "\n";
# Convert all keys to uppercase
my %upper_config = map { uc $_ => $config{$_} } sort keys %config;
print join(", ", sort keys %upper_config), "\n";
This demonstrates converting hash keys to uppercase for case-insensitive
lookups. We use both uc and lc for normalization.
$ ./hash.pl Value: 30 DEBUG, RETRIES, TIMEOUT
File Processing
uc can standardize text when processing files.
#!/usr/bin/perl
use strict;
use warnings;
use v5.34.0;
open(my $fh, '<', 'data.txt') or die "Can't open file: $!";
while (my $line = <$fh>) {
chomp $line;
print uc($line), "\n";
}
close($fh);
This script reads a file line by line, converts each line to uppercase,
and prints it. Combining chomp and uc is common.
Unicode Handling
uc properly handles Unicode characters when using appropriate
Perl versions and settings.
#!/usr/bin/perl use strict; use warnings; use v5.34.0; use utf8; my $text = "café résumé"; my $upper = uc($text); binmode STDOUT, ':utf8'; print "Original: $text\n"; print "Uppercase: $upper\n";
With use utf8, Perl correctly handles non-ASCII characters.
The output shows proper conversion of accented letters to uppercase.
$ ./unicode.pl Original: café résumé Uppercase: CAFÉ RÉSUMÉ
Best Practices
- Preserve originals: Create new variables for uppercase versions.
- Prefer Unicode strings: Decode input and use utf8 rather than relying on locale.
- Combine with lc: For case-insensitive comparisons.
- Handle Unicode: Use utf8 pragma for non-ASCII text.
- Document conversions: Make case changes obvious in code.
Source
This tutorial covered Perl's uc function with practical
examples demonstrating its usage in various scenarios.
Author
List all Perl tutorials.