in reply to Why doesn't exist work with hash slices?

It isn't obvious that exists @haystack{qw(aa bb cc dd)} should provide an "ANY" test. Some might think an "ALL" test would be correct. Thus, it definitely doesn't belong in core.

I'd suggest using any for clarity and to remove the need for !! (and the risk that one of your keys might be "0").

use List::Util qw(any first); my %haystack; @haystack{'aa'..'ff',"0"} = (); say any { exists $haystack{$_} } qw(aa bb cc dd); say any { exists $haystack{$_} } qw(zz 0); # OK say !! first { exists $haystack{$_} } qw(zz 0); # Oops!

Good Day,
    Dean

Replies are listed 'Best First'.
Re^2: Why doesn't exist work with hash slices?
by LanX (Saint) on Sep 21, 2025 at 13:13 UTC
    > exists @haystack{qw(aa bb cc dd)} should provide an "ANY" test. Some might think an "ALL" test would be correct.

    In order to respect orthogonality to delete I'd expect a _list_¹ of booleans to be returned.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

    ¹) emphasize updated

      According to perldoc -f delete, it returns the deleted value, not a Boolean. And,

      $ perl -E 'my %foo = qw{ bar 42 }; say delete $foo{bar};'
      

      in fact prints 42.

        LanX isn't saying that delete ELEMENT returns a boolean; they're saying that since delete SLICE returns a list of what it would return for a single element (the deleted element), exists SLICE should also return a list of what it return for a single element (a boolean).

        Delete returns the deleted value*s* (plural) of a slice and you didn't test a slice. ¹

        Exists returns an (informal) Boolean (1 or dualvar ""/0) for a hash entry, and logically should also return a list of booleans in case of a slice.

        Cheers Rolf

        (addicted to the Perl Programming Language :)
        see Wikisyntax for the Monastery

        Edit

        ¹) From the docs of delete I already linked to

        my %hash = (foo => 11, bar => 22, baz => 33); my $scalar = delete $hash{foo}; # $scalar is 11 $scalar = delete @hash{qw(foo bar)}; # $scalar is 22 my @array = delete @hash{qw(foo baz)}; # @array is (undef,33) <--- H +ERE