To check if a string is empty in Perl, you can use the length
function.
In this example,
$str1
with an empty string value ""
.length
function on $str1
to get the length of the string.0
, the string is empty.$str1 = "";
$isEmpty = length($str1) == 0;
print "Is string empty: $isEmpty";
Is string empty: 1
In this example,
$str2
with a non-empty string value "Hello World"
.length
function on $str2
to get the length of the string.0
, the string is empty.$str2 = "Hello World";
$isEmpty = length($str2) == 0;
print "Is string empty: $isEmpty";
Is string empty: 0
In this tutorial, we learned How to Check if String is Empty in Perl language with well detailed examples.