Home > Back-end >  Can Perl's smart match be used to validate a list of fields?
Can Perl's smart match be used to validate a list of fields?

Time:06-20

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:

  • @valid_opts = (qw/a b c/)
  • %opts = (a => 1, b => 2)

This should be false:

  • @valid_opts = (qw/a b c/)
  • %opts = (a => 1, b => 2, d => 4)

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?

CodePudding user response:

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: Key [LasstName] 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.

  • Related