package Phone::Type;
my $types;
BEGIN {
$types = {
HOME => 'Home',
WORK => 'Work',
};
}
use constant $types;
sub is_phone_type {
my ($type) = @_;
return exists $types->{$type};
}
package main;
use Carp ();
sub fun
{
my ($my_phone_type_enum) = @_;
Phone::Type::is_phone_type( $my_phone_type_enum)
or Carp::croak "Invalid type $my_phone_type_enum";
}
fun(Phone::Type->HOME);
fun(Phone::Type->WORK);
fun('DOG');
(BEGIN needs to set $ types at compile time so that it is available for use.)
In Perl, it is quite common to be relaxed in such things; just assume that functions are passed to numbers where you expect numbers, etc. But if you want to do such a check, you might be interested in Params :: Validate .
source
share