Invalid value

| Perl | 6 Comments

Let's say that you need to check the validity of some input value. If it is found in a list of acceptable values then you continue otherwise you need to croak some error message.

For starters let's assume that the list of acceptable values is contained in the following array:

my @list = qw(email name telephone address);

Also don't forget that in order to be able to croak you need to include use Carp; in the file.

Here's the most straight forward way to do it:

sub func
{
    my $value = shift;
    my $found = 0;
    for my $next (@list)
    {
        if ($next eq $value)
        {
            $found++;
            last;
        }
    }
    croak "Invalid value '$value'" unless $found;
    ...
}

Here's another especially clever albeit slightly cryptic way to do it[1]:

sub func
{
    my $value = shift;
    { map { $_ => 1 } @list }->{$value}
        or croak "Invalid value '$value'";
    ...
}

Finally, if you are familiar with the List::MoreUtils module, then here's yet another way to do it:

use List::MoreUtils qw/any/;

sub func
{
    my $value = shift;
    croak "Invalid value '$value'";
        unless any { $value eq  $_ } @list
    ...
}

As you can see there are many correct ways to do it and probably many more.

[1] Perl Medic p. 122

6 Comments

If you are using Perl 5.10 you can also use the smart match operator:

I usually find hashes to be the natural choice for lookups:

my @list = qw(email name telephone address);
my @lookup{@list} = (1) x @list;

sub func
{
my $value = shift;
croak "Invalid value '$value'" unless $lookup{$value};
...
}

(BTW, with javascript disabled, this page displays an anonymous comment form, but rejects any anonymous comment submissions, saying only "invalid request". You might want to either enable anonymous comments or work up a way to prevent the form from being displayed when javascript is off.)

Although I would prefer your last example for clarity , here's another way to do it :)

  1 use List::AllUtils qw/reduce/;
  2 use Test::More 'no_plan';
  3 use Carp;
  4 use feature 'say';
  5 my @list = qw(email name telephone address);
  6
  7 sub func {
  8     my $value = $_[0];
  9     reduce {
 10         $a or ($b eq $value);
 11     } (0,@list)
 12     or croak "Invalid value '$_[0]'";
 13 }
 14
 15 ok( !eval{ func('dog')   } , 'not contained test' );
 16 ok(  eval{ func('email') } , 'contained test'     );
 17 # if I didn't use the eval it wouldn't pass the first test :)

Random entries

Here are some random entries that you might be interested in:

Recent Assets

  • tegen-par-2024-2nd-place.jpeg
  • stanford-reunion.png
  • kiff.png
  • hoid.png
  • Dad-in-front-of-log-cabin.png
  • mistborn-trilogy.png
  • 2024-03-Heren1-27h.png
  • three-body-problem.png
  • 10CC.png
  • minds-and-machines.jpeg
  • puglia.png
  • 2023-09-24-jong-tegen-oud-1.jpg

Recent Comments

  • Invalid value: You're right, List::Utils can accomplish alot of s ...
    - Kiffin
  • Invalid value: Although I would prefer your last example for clar ...
    - Stefan Petrea
  • Invalid value: The problem with enabling anonymous comments is th ...
    - Kiffin
  • Invalid value: I usually find hashes to be the natural choice for ...
    - nomaddervish
  • Invalid value: If you are using Perl 5.10 you can also use the sm ...
    - zby

Golf Handicap

Information

This personal weblog was started way back on July 21, 2001 which means that it is 7-21-2001 old.

So far this blog contains no less than 2518 entries and as many as 1877 comments.

Important events

Graduated from Stanford 6-5-1979 ago.

Kiffin Rockwell was shot down and killed 9-23-1916 ago.

Believe it or not but I am 10-11-1957 young.

First met Thea in Balestrand, Norway 6-14-1980 ago.

Began well-balanced and healthy life style 1-8-2013 ago.

My father passed away 10-20-2000 ago.

My mother passed away 3-27-2018 ago.

Started Gishtech 04-25-2016 ago.