How to Check String is empty or not in Perl with code example

This tutorial explores multiple methods to determine whether a given string is empty or not, accompanied by Perl code examples.

  • using string comparison operator, eq and ne, $str eq "" or numeric comparison operator $str==""
  • Another way using length function. length($str) == 0

Using String Comparison Operators: eq and ne

Perl offers the following string comparison operators:

  • eq: The equal operator checks if the given string matches another string. It returns true if there’s a match, otherwise false.
  • ne: The not equal operator is the reverse of eq. It returns false if there’s a match, otherwise true.

Example: Checking for Empty String using eq Operator

$str = "";
if ($str eq "") {
    print "String is empty\n";
}

Output:

String is empty

Similarly, we can utilize the ne operator to check if a string is not empty.

$str = "";
if ($str ne "") {
    print "String is not empty\n";
} else {
    print "String is empty\n";
}

Output:

String is empty

Checking for Empty Strings using Numeric Comparison Operators

Perl provides numeric comparison operators to check if a string is empty or not.

  • == The equal operator checks if the given string matches another string.
  • != The not equal operator is the reverse of == operator.

String check blank using == operator example

string length is zero using == operator.

$str = "";
if (length($str) == 0) {
    print "String is empty\n";
}

Output:

String is empty

Similarly, we can use the != operator to check if a string is not empty.

$str = "";
if (length($str) != 0) {
    print "String is not empty\n";
} else {
    print "String is empty\n";
}

Output:

String is empty

Using the length Function in Perl

Another approach to check for empty strings in Perl is by utilizing the length function.

The length function returns the number of characters in a string. An empty string contains no characters, hence length returns zero.

use the length of a string code in a conditional if statement. Here is an example

$str = "";
if (length($str) == 0) {
    print "String is empty and length is zero\n";
}

Using regular expression

Another method involves using regular expressions. A regular expression consists of a pattern used to match text.

The pattern /^\s*$/ is composed of the following elements:

  • ^ - Denotes the start of the line.

  • \s* - Matches zero or more whitespace characters (including spaces or tabs). \s matches any whitespace character, and * represents zero or more occurrences

  • $ - Indicates the end of the line.

The =~ operator is a matching operator used to test a string against a regular expression. It is typically used within a conditional if statement to execute code based on whether the match is successful or not.

$emptyString = "";
if ($emptyString =~ /^\s*$/) {
    print "String is empty and length is zero\n";
}

Conclusion

In summary, various approaches are utilized:

  • Length Function: The length function is easy to use and is commonly employed. It offers simplicity and is widely understood.

  • Regular Expressions: While regular expressions provide flexibility, understanding the pattern can be a bit complex. For simple empty checks, this approach may be considered overkill. However, it becomes valuable for more complex validations.

  • Perl Comparison Operators: Perl comparison operators are straightforward to grasp and widely understood. However, familiarity with their usage is essential.

In general, Using the length function is the preferred approach for checking if a string is empty or not.