return SvTRUE(sv);
}
}
- if (av_fetch(av, key, 0))
+ if (key <= AvFILLp(av) && AvARRAY(av)[key] != &PL_sv_undef
+ && AvARRAY(av)[key])
+ {
return TRUE;
+ }
else
return FALSE;
}
The exists() and delete() builtins now work on simple arrays as well.
The behavior is similar to that on hash elements.
-exists() can be used to check whether an array element exists without
-autovivifying it. If the array is tied, the EXISTS() method in the
-corresponding tied package will be invoked.
-
-delete() may now be used to remove an element from the array and return
-it. If the element happens to be the one at the end, the size of the
-array also shrinks by one. If the array is tied, the DELETE() method
+exists() can be used to check whether an array element has been
+initialized without autovivifying it. If the array is tied, the
+EXISTS() method in the corresponding tied package will be invoked.
+
+delete() may be used to remove an element from the array and return
+it. The array element at that position returns to its unintialized
+state, so that testing for the same element with exists() will return
+false. If the element happens to be the one at the end, the size of
+the array also shrinks by one. If the array is tied, the DELETE() method
in the corresponding tied package will be invoked.
See L<perlfunc/exists> and L<perlfunc/delete> for examples.
if you are certain that you're calling the function correctly, you may put
an ampersand before the name to avoid the warning. See L<perlsub>.
-=item %s argument is not a HASH element
+=item %s argument is not a HASH or ARRAY element
-(F) The argument to exists() must be a hash element, such as
+(F) The argument to exists() must be a hash or array element, such as:
$foo{$bar}
- $ref->[12]->{"susie"}
+ $ref->[12]->["susie"]
-=item %s argument is not a HASH element or slice
+=item %s argument is not a HASH or ARRAY element or slice
-(F) The argument to delete() must be either a hash element, such as
+(F) The argument to delete() must be either a hash or array element, such as:
$foo{$bar}
- $ref->[12]->{"susie"}
+ $ref->[12]->["susie"]
-or a hash slice, such as
+or a hash or array slice, such as:
- @foo{$bar, $baz, $xyzzy}
+ @foo[$bar, $baz, $xyzzy]
@{$ref->[12]}{"susie", "queue"}
=item %s did not return a true value
a hash tied to a DBM file deletes the entry from the DBM file. Deleting
from a C<tie>d hash or array may not necessarily return anything.
+Deleting an array element effectively returns that position of the array
+to its initial, uninitialized state. Subsequently testing for the same
+element with exists() will return false. See L</exists>.
+
The following (inefficiently) deletes all the values of %HASH and @ARRAY:
foreach $key (keys %HASH) {
delete $ref->[$x][$y][$index];
delete @{$ref->[$x][$y]}[$index1, $index2, @moreindices];
-
=item die LIST
Outside an C<eval>, prints the value of LIST to C<STDERR> and
=item exists EXPR
Given an expression that specifies a hash element or array element,
-returns true if the specified element exists in the hash or array,
-even if the corresponding value is undefined. The element is not
-autovivified if it doesn't exist.
+returns true if the specified element in the hash or array has ever
+been initialized, even if the corresponding value is undefined. The
+element is not autovivified if it doesn't exist.
print "Exists\n" if exists $hash{$key};
print "Defined\n" if defined $hash{$key};
print "Defined\n" if defined $array[$index];
print "True\n" if $array[$index];
-A hash element can be true only if it's defined, and defined if
+A hash or array element can be true only if it's defined, and defined if
it exists, but the reverse doesn't necessarily hold true.
Note that the EXPR can be arbitrarily complicated as long as the final