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)
    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


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);
  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 }
 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 :)

Leave a comment

Random entries

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

Recent Assets

  • flitsfoto-B5H3-IoN-b9E4tr9OrFarmWfWrP5pxuN8AgYw45ABAnqqDrmjSqIc-KKoAQ9Bu_k.jpeg
  • flitsfoto-mjuWdZ1wBk6gJbu9rP7Bu6bETxZplWXFUoEJIA_HfMF0i0MQfgF_y5024mEsOBfH.jpeg
  • Kiffin-10th-hole-2017-05-small.jpg
  • Kiffin-10th-hole-2017-05.jpg
  • ember-rails-and-json-api.png
  • putting-mat.png
  • finished-just-in-time.png
  • Tenerife-flight-path.png
  • listen-to-me-marlon.jpg
  • sneeuwval.jpg
  • frozen-crystals.png

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


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 2288 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.

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

My father passed away 10-20-2000 ago.

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