In Perl, you can check if an array is empty by using the scalar
context or checking its size.
In this example,
@numbers
with some elements.scalar(@numbers)
in scalar context or checking its size.@numbers = (1, 2, 3);
$is_empty = scalar(@numbers) == 0;
print "Array is empty: $is_empty\n";
Array is empty: false
In this example,
@empty_array
.scalar(@empty_array)
in scalar context or checking its size.@empty_array = ();
$is_empty = scalar(@empty_array) == 0;
print "Array is empty: $is_empty\n";
Array is empty: true
In this tutorial, we learned How to check if an Array is Empty in Perl language with well detailed examples.