From: Jacob Bachmeyer Date: Sat, 12 Nov 2022 02:24:28 +0000 (-0600) Subject: Add check for scalar context in find_directive_elements X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=1a9d600c1695ffa47becd2f2a6a3c574f82be817;p=gatekeeper.git Add check for scalar context in find_directive_elements This avoids building a list when the only important detail is whether a key is present in the directive. --- diff --git a/gatekeeper.pl b/gatekeeper.pl index 3822701..e57e5fc 100755 --- a/gatekeeper.pl +++ b/gatekeeper.pl @@ -1434,19 +1434,28 @@ sub read_directive_from_string { =item @values = find_directive_elements ( $directive, $key ) +=item $count = find_directive_elements ( $directive, $key ) + Search the DIRECTIVE arrayref for KEY elements and return their associated values. An empty list is returned if no KEY elements are found in DIRECTIVE. The KEY comparison is case-insensitive. The values returned from this procedure are tainted. +In scalar context, return the number of entries that would be returned in +list context. + =cut sub find_directive_elements { my $directive = shift; my $key = lc shift; - return map $_->[1], grep lc($_->[0]) eq $key, @$directive; + if (wantarray) { + return map $_->[1], grep lc($_->[0]) eq $key, @$directive; + } else { + return grep lc($_->[0]) eq $key, @$directive; + } } =item $directory = find_directory ( $directive )