From 1a9d600c1695ffa47becd2f2a6a3c574f82be817 Mon Sep 17 00:00:00 2001 From: Jacob Bachmeyer Date: Fri, 11 Nov 2022 20:24:28 -0600 Subject: [PATCH] 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. --- gatekeeper.pl | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) 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 ) -- 2.25.1