Reputation: 1921
Usually I have a hash of %valid_opts = (a => 1, b => 2, ...)
for each valid option that could be passed to a function and then I iterate to find out if an option was passed that wasn't supported. This saves me from myself when a typo happens and I wonder why something is broken:
sub f
{
my %opts = @_;
my %valid_opts = (a => 1, b => 1, c => 1);
foreach (keys(%opts)) {
croak "bad option $_" if !$valid_opts{$_};
}
# profit
}
Recently I learned about the ~~
smart match operator and wondered if it could simplify the job:
Can I use the ~~
operator to check if %opts
contains only keys that exist in %valid_opts
without a loop?
I've tried a few combinations of things like keys(%opts) ~~ @valid_opts
but I think its comparing to make sure they match exactly. If this is possible, then how would you set it up?
This should be true:
This should be false:
At the risk that this question is really an XY problem, then is there a better way to check for valid options at the top of a function?
Upvotes: 2
Views: 91
Reputation: 4488
There are modules that can do this, and are more powerful. But otherwise, you also can create very easily a function to do this on your own. A full example.
I named the file validation.pl in my example.
#!/usr/bin/env perl
use strict;
use warnings;
use v5.32;
use Carp qw(croak);
# Works Fine
helloA(
Name => "David",
LastName => "Raab",
);
helloB(
Name => "David",
LastName => "Raab",
);
# Throws: Keys [LasstName, Invalid] not supported by main::valid_arguments at ./validation.pl line 19.
helloA(
Name => "David",
LasstName => "Raab",
);
# Would also throw exception.
helloB(
Name => "David",
LasstName => "Raab",
);
# valid_arguments(["Name", "LastName"], @_);
sub valid_arguments {
my ($valids, %orig) = @_;
# Turns: ["A","B","C"] into {"A" => 1, "B" => 1, "C" => 1}
my %valids = map { $_ => 1 } @$valids;
# Check all passed arguments
for my $key (keys %orig) {
# if they are valid entry
if ( exists $valids{$key} ) {
# when true - do nothing
}
else {
# when false - throw error
local $Carp::CarpLevel = 2;
my @caller = caller 0;
croak (sprintf "Key [%s] not supported by %s", $key, $caller[3]);
}
}
# returns hash in list context. hashref in scalar context.
return wantarray ? %orig : \%orig;
}
sub helloA {
my (%args) = valid_arguments(["Name", "LastName"], @_);
printf "Hello %s %s\n", $args{Name}, $args{LastName};
}
sub helloB {
my $args = valid_arguments(["Name", "LastName"], @_);
printf "Hello %s %s\n", $args->{Name}, $args->{LastName};
}
With caller()
you can get information from which other source your function is called. Carp::croak()
throws an exception from another perspective. So you get an error-message where a user needs it to fix his error.
EDIT:
An extended valid_arguments
that checks for all arguments.
# Examples:
# my %args = valid_arguments(["Name", "LastName"], @_);
# my $args = valid_arguments(["Name", "LastName"], @_);
sub valid_arguments {
my ($valids, %orig) = @_;
# Turns: ["A","B","C"] into {"A" => 1, "B" => 1, "C" => 1}
my %valids = map { $_ => 1 } @$valids;
# Get a list of invalid arguments
my @invalids;
for my $key ( keys %orig ) {
push @invalids, $key if not exists $valids{$key};
}
# Throw error if any invalid exist
if ( @invalids > 0 ) {
local $Carp::CarpLevel = 2;
my $caller = (caller 0)[3];
@invalids == 1
? croak (sprintf "Key [%s] not supported by %s", $invalids[0], $caller)
: croak (sprintf "Keys [%s] not supported by %s", join(", ", @invalids), $caller);
}
# return hash in list context. hashref in scalar context.
return wantarray ? %orig : \%orig;
}
Currently all arguments are optional. How about mandatory? You can add this feature too, but I would use a already develeoped and used module instead.
Upvotes: 3