Perl lc Function

These sites are FOR SALE:,!
If you are interested, please use my Contact page.

The Perl lc function (lc stands for lowercase) accepts as parameter an expression and returns a lowercase version of that expression. In this short tutorial we’ll give you some useful examples that show you how to use this function in your script applications.

There is of course the function uc (uppercase) which returns an uppercase value of a string expression. Click the uc link to see what uc function is about.

We have two syntax forms for this function:

lc EXPR 

The Perl lc function doesn’t change the content of a EXPR string variable, it merely returns a lowercase copy of the string. If the EXPR is omitted, the function is called against the special variable $_.

Click Below to See the Best

Perl How-to Snippet Collection

and Save Hours of Surfing on Internet!

A simple example about how to use it:

# initialize some string variable
my $str = "What is Perl Language for";
print $str, "\n";
# displays: What is Perl Language for
$str = lc($str);
print $str, "\n";
# displays: what is perl language for

In the third line of this snippet code, we converted $str to lowercase, but without an assignment our conversion was lost. The next lines of the code show you how to do it.

Perl lc function is very useful when you need to perform some string comparisons against some string variables. Bear in mind that all string comparisons are case sensitive and the following strings "Perl" and "perl" are not the same. As a parenthesis, don’t ever use numerical operators in string comparisons, because it’s for sure the result will not be the one you would expect.

Now let’s get back to our comparisons. If you want to compare two string variables case insensitive using lc function, you may have at least two choices:

  • Convert both strings in lowercase using an assignment operator and than make the comparisons
  • Compare the strings by calling the lc function for each of them, without changing their values
See the next short example about how to do this:

#comparison using the assignment operator
my $str1 = " rose";
my $str2 = " ROSE";
$str1 = lc($str1);
$str2 = lc($str2);
if($str1 eq $str2) {
  print "The two strings are equal\n";

#comparison without altering the string values

$str1 = " rose";
$str2 = " ROSE";
if(lc($str1) eq lc($str2)) {
  print "The two strings are equal\n";

If you want to download the Perl lc script with all the above examples included, please click here: Script download


Through these exercises you have the opportunity to try yourself to write some script code where you can use the Perl lc function. These exercises are completely covered in my Perl "Perl Scalar and String Functions - How To Tutorial" where I show you how to play with this important function in detail.
1. Write a few lines of code to show how to compare two strings case insensitive.
2. Give an example how to convert all the elements of an array in lowercase.

A-N-Y-O-N-E Can Learn and Master Perl!
And That Includes YOU!

Check these how-to tutorial eBooks (PDF format):

Table of Contents:

A Perl Script
Install Perl
Running Perl
Perl Data Types
Perl Variables
Perl Operators
Perl Lists
Perl Arrays
    Array Size
    Array Length
Perl Hashes
Perl Statements
    Perl if
    Perl unless
    Perl switch
    Perl while
    Perl do-while
    Perl until
    Perl do-until
    Perl for
    Perl foreach
Built-in Perl Functions
    Functions by Category
        String Functions
        Regular Expressions and Pattern Matching
        List Functions
        Array Functions
        Hash Functions
        Miscellaneous Functions
    Functions in alphabetical order
        lc (more)

return from Perl lc Function to Perl Basics

Would you like to create your own website like this one?
Hit the Alarm Clock!

Site Build It!