Perl exists Function



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:

exists $hash{$key}

where %hash is a hash table and $key is an element whose existence is checked.


Click Below to See the Best

Perl How-to Snippet Collection

and Save Hours of Surfing on Internet!


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.

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:

exists $array{$i}

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:

exists &subroutine

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.

A-N-Y-O-N-E Can Learn and Master Perl!
And That Includes YOU!


Check these how-to tutorial eBooks (PDF format):


Table of Contents:

A Perl Script
Install Perl
Running Perl
Perl Data Types
Perl Variables
Perl Operators
Perl Lists
Perl Arrays
    Array Size
    Array Length
Perl Hashes
Perl Statements
    Perl if
    Perl unless
    Perl switch
    Perl while
    Perl do-while
    Perl until
    Perl do-until
    Perl for
    Perl foreach
Built-in Perl Functions
    Functions by Category
        String Functions
        Regular Expressions and Pattern Matching
        List Functions
        Array Functions
        Hash Functions
        Miscellaneous Functions
    Functions in alphabetical order
        chomp
        chop
        chr
        crypt
        defined
        delete
        each
        exists (more)
        grep
        hex
        index
        join
        keys
        lc
        lcfirst
        length
        map
        oct
        ord
        pack
        pop
        push
        q
        qq
        qw
        reverse
        rindex
        scalar
        shift
        sort
        splice
        split
        sprintf
        substr
        tr
        uc
        ucfirst
        undef
        unpack
        unshift
        values

return from Perl exists Function to Perl Basics



Would you like to create your own website like this one?
Hit the Alarm Clock!

Site Build It!