From f46862e41025e6d96509056229c28a90eb0ceb40 Mon Sep 17 00:00:00 2001 From: Andrew DeFaria Date: Tue, 22 Jun 2021 17:12:48 -0700 Subject: [PATCH] Added RequiredFields --- lib/Utils.pm | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/lib/Utils.pm b/lib/Utils.pm index 446d2e7..b9be1e4 100644 --- a/lib/Utils.pm +++ b/lib/Utils.pm @@ -79,6 +79,7 @@ our @EXPORT = qw ( PipeOutput PipeOutputArray ReadFile + RequiredFields RedirectOutput StartPipe Stats @@ -979,6 +980,67 @@ Returns: exit 1; } # Usage +sub RequiredFields($$) { + +=pod + +=head2 RequiredFields($total, $log) + +Check if a list of fields are contained in a hash + +Parameters: + +=for html
+ +=over + +=item $fields + +Array reference to a list of field names that are required + +=item $rec + +Hash reference whose key values we are checking + +=back + +=for html
+ +Returns: + +=for html
+ +=over + +=item Message + +Returns either an empty string or a string naming the first missing required +field + +=back + +=for html
+ +=cut + + my ($fields, $rec) = @_; + + for my $fieldname (@$fields) { + my $found = 0; + + for (keys %$rec) { + if ($fieldname eq $_) { + $found = 1; + last; + } # if + } # for + + return "$fieldname is required" unless $found; + } # for + + return; +} # RequiredFields + END { StopPipe; } # END -- 2.17.1