1 # Copyright (C) 2007-2009, Parrot Foundation.
6 The C<exists> opcode tells you whether an element of a container PMC
7 (array or hash for example) exists. It differs from C<defined>, because
8 C<defined> will return false on undefined values, which are valid entries.
10 It's important to think of an array or hash as being a collection of data
11 buckets. That bucket can exist but not contain a defined value. If the
12 bucket does not exist, there is no value to check.
14 In the example below, C<my_array[0]> exists and is defined, but
15 C<my_array[1]> only exists without ever being given a defined value.
16 C<my_array[2]> does not exist and is thus not defined. Similar to
17 C<defined>, the behavior of C<exists> for a given PMC depends on how
18 that PMC implements the related vtable functions.
24 # set up an array with two elements
26 my_array = new ['ResizablePMCArray']
33 # looking at the first element
34 $I1 = defined my_array[0]
35 unless $I1 goto not_def_0
36 say "my_array[0] is defined"
37 goto end_check_defined_0
39 say "my_array[0] is not defined"
42 $I2 = exists my_array[0]
43 unless $I2 goto not_exists_0
44 say "my_array[0] exists"
45 goto end_check_exists_0
47 say "my_array[1] does not exist"
51 # looking at the second element
52 $I3 = defined my_array[1]
53 unless $I3 goto not_def_1
54 say "my_array[1] is defined"
55 goto end_check_defined_1
57 say "my_array[1] is not defined"
60 $I4 = exists my_array[1]
61 unless $I4 goto not_exists_1
62 say "my_array[1] exists"
63 goto end_check_exists_1
65 say "my_array[1] does not exist"
69 # looking at the third element
70 $I5 = defined my_array[2]
71 unless $I5 goto not_def_2
72 say "my_array[2] is defined"
73 goto end_check_defined_2
75 say "my_array[2] is not defined"
78 $I6 = exists my_array[2]
79 unless $I6 goto not_exists_2
80 say "my_array[2] exists"
81 goto end_check_exists_2
83 say "my_array[2] does not exist"
92 # vim: expandtab shiftwidth=4 ft=pir: