The Perl exists function can be used in conjunction with hashes, arrays or subroutines. Generally, you can use it in conditional expressions, to check whether a particular hash/array element or a subroutine exists.If you use it with a hash, you can check if a table entry exists in the hash. In this case the Perl exists function returns true whether the specified element in the hash has been initialized and false otherwise.
The syntax form is as follows:
where %hash is a hash table and $key is an element whose existence is checked.You can see a short example here:
#!/usr/local/bin/perl
use strict;
use warnings;
my (%hash, $key);
%hash = ( 1 => 'one', 2 => 'two',
3 => 'three', 4 => 'four', 5 => undef );
print "found 1\n" if exists $hash{1};
print "found 5\n" if exists $hash{5};
print "the key 6 doesn't exist\n" if !exists $hash{6};
exists $hash{7} || print "the key 7 doesn't exist\n";
You can see that this code returns true if you check the element having as key the number 5, even though it is undefined. It returns false only if the element (in our example 6 and 7) is not initialized yet.
Check my new How To Tutorial eBooks (PDF format):to see a lot of examples about how to use built-in functions, complex data structures and statements in Perl.
For array elements this function is deprecated and can be removed in the future versions. But in Perl 5.12 version it is still available, so we’ll stick around a bit. You can use it after the delete function.As you know, the delete function on an array is used to delete the value of a particular index – it does not remove the element, but instead it replaces the element with undef. You can use the Perl exists function to check if the value of an element was deleted by the delete function.
The syntax form is as follows:
where $array[$i] is the $ith element of the @array. It returns false if you deleted the element with delete and true if you initialized the element with undef or with any other value.See a short snippet below:
#!/usr/local/bin/perl
use strict;
use warnings;
my @array = (1..5);
delete $array[2];
print "\@array has ", $#array+1, " elements\n";
# it prints @array has 5 elements
foreach (0..$#array) {
print $array[$_], ' ' if exists $array[$_];
}
print "\n";
# it prints: 1 2 4 5
As you can see, the middle element of the array was "deleted" and its value was assigned with undef. You can check this by using the Perl exists function.
The exists function can be used to check if a named subroutine has ever been declared. In this case, you can use the following syntax form:
It returns true if the subroutine was declared and false otherwise.The following short code snippet shows how you can use the Perl exists function with subroutines:
#!/usr/local/bin/perl
use strict;
use warnings;
sub subtract;
sub add {
return shift() + shift();
}
print "divide doesn't exist\n" if !exists ÷
print "subtract exists\n" if exists &subtract;
print "subtract it's not defined\n" if !defined &subtract;
print "add is defined\n" if defined &add;
print "5 + 7 = ", &add(5,7), "\n" if(defined &add);
This code outputs:divide doesn't exist
subtract exists
subtract it's not defined
add is defined
5 + 7 = 12
At the same time, in this example you can notice the difference between defined and exists.
NEW!!!
Do you want more information about the basic Perl topics?
Check my new "Perl How To" Tutorial eBooks page where I'll answer the most frequent questions regarding some topics :