Syntax
The syntaxes of the length () function of PERL is given below.
- length
The length() function without any argument will return the length of the variable, $_. - length(variable)
The length() function with the argument variable will return the length of the argument variable.
Different uses of the PERL script have been shown in the next part of this tutorial.
Example-1: Count the length of the $_ variable
Create a PERL file with the following code that will count the total number of characters of the string value stored in the scalar variable. A string value has been stored in the scalar variable. The print operator has been used to print the value of the scalar variable. The length() function without any argument has been used to count the length of the scalar variable. Next, the return value of the length() function has been printed.
# Define the special variable $_
$_ = "Welcome to LinuxHint\n";
# Print the value of $_
print;
# Count the length of $_
$total = length;
# Print the total number of characters
print "Total number of characters : $total\n";
Output:
The following output will appear after executing the above code.
Example-2: Count the Length of the String Variable
Create a PERL file with the following code that will count the total number of characters of the string variable. The username and the password will be taken from the user and stored in two variables, $username and $password. The chomp() function has been used to remove the newline from the variables. The length() function has been used here to count the length of the $username and $password values. If the length of the $username is less than 6, then the message, “Username must contain 6 characters.” will be printed. If the length of the $password is less than 6 and greater than 12, then the message, “Password can be 6 to 12 characters long.” will be printed. After validating the input values, if the value of the $username is “fahmida” and the $password is “secret,” then a success message will be printed; otherwise, a failure message will be printed.
print "Enter username:\n";
$username = < >;
# Remove the newline character
chomp($username);
print "Enter password:\n";
$password = < >;
# Remove the newline character
chomp($password);
# Check the length of the $username variable
if(length($username) < 6)
{
die "Username must contain 6 characters.\n";
}
if(length($password) < 6 || length($password) > 12)
{
die "Password can be 6 to 12 characters long.\n";
}
# Check the length of the $password variable
if($username eq 'fahmida' && $password eq 'secret')
{
print "Authenticated user.\n";
}
else
{
print "Unauthenticated user.\n";
}
Output:
The following output will appear after executing the code with the valid username but invalid password inputs. The input value, “fahmida,” is valid data and the correct username. The input value, “1234”, is invalid data.
The following output will appear after executing the code with the invalid username but valid password. The input value, “admin,” is invalid data. The input value, “secret,” is valid data and the correct password.
The following output will appear after executing the code with the valid username but incorrect password. The input value, “fahmida,” is valid data and the correct username. The input value, “123456”, is valid data but an incorrect password.
The following output will appear after executing the code with the correct username and password. The input value, “fahmida,” is valid data and the correct username. The input value, “secret,” is valid data and the correct password.
Example-3: Count the Length of the String Variable in Bytes
You have to use the bytes module to count the length of the strings in bytes. Create a PERL file with the following code that uses the bytes module to count the length of string data given in hexadecimal format. The original string value and the number of bytes of that string value will be printed later.
# Define string data by hex value
$data = "\x{35}\x{250}\x{245}\n";
# Print the string value of hex data
print "The string value is ", $data;
# Print the length of the string in bytes
print "The length of the string is: ", length($data), " bytes.", "\n";
Output:
The following output will appear after executing the code.
Example-4: Count the Length of Each Array Element
Create a PERL file with following code that will count the length of each string value of the array. An array of 4 string values has been defined in the code. The foreach loop has been used here to iterate the array values and, count and the length of each array value.
@usernames = ("nupur22", "hossainbd", "kamal12", "bela89");
# Iterate the array values using for loop
for($i = 0; $i <= $#usernames; $i++)
{
# Count the length of each array value
$len = length($usernames[$i]);
# Print the length of the array value
print "The length of $usernames[$i] is $len\n";
}
Output:
The following output will appear after executing the code.
Conclusion
The uses of the length() function to count the length of the string value and apply the output of the length() function for different purposes has been shown in this tutorial by using PERL examples. I hope this tutorial will help the PERL users to use the length() function for counting the length of the string in their code properly.