Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I validate enum types as Perl subroutine arguments?

Building off Does Perl have an enumeration type?, how can I perform dynamic type checking (or static type checking if use strict is able to do so) that my subroutine argument is getting the right type of enum?

package Phone::Type;

use constant {
    HOME => 'Home',
    WORK => 'Work',
};

package main;

sub fun
{
    my ($my_phone_type_enum) = @_;
    # How to check my_phone_type_enum, is either Phone::Type->HOME or Phone::Type->WORK or ... but not 'Dog' or 'Cat'?
}

fun(Phone::Type->HOME); # valid
fun(Phone::Type->WORK); # valid
fun('DOG');             # run-time or compile time error
like image 812
Cheok Yan Cheng Avatar asked Jan 28 '26 08:01

Cheok Yan Cheng


2 Answers

Here is one way:

#!/usr/bin/perl

package Phone::Type;

use strict;
use warnings;

use constant {
    HOME => 'Home',
    WORK => 'Work',
};

package main;

use strict;
use warnings;

sub fun {
    my ($phone_type) = @_;
    Phone::Type->can( $phone_type )
        or die "'$phone_type' is not valid\n";
}

fun('HOME'); # valid
fun('WORK'); # valid
fun('DOG');  # run-time or compile time error
__END__

C:\Temp> dfg
'DOG' is not valid
like image 104
Sinan Ünür Avatar answered Jan 30 '26 20:01

Sinan Ünür


I would suggest that you use Readonly (as suggested in the referenced question) rather than constant. I'd suggest on of two possible approaches (depending on if you are using Perl 5.10 or 5.8).

Initially, the same code:

use strict;
use warnings;
use Readonly;

Readonly my @phone_types = qw/HOME WORK/;

Perl 5.10:

sub fun
{
   my $type = shift;
   die "Invalid phone type: $type" unless $type ~~ @phone_types;
   # ...
}

Perl 5.8:

sub fun
{
   my $type = shift;
   die "Invalid phone type: $type" unless grep { $_ eq $type} @phone_types;
   # ...
}

There is a module on CPAN which will allow you to have a great deal of control over argument types and values but I can't for the life of me remember it. Perhaps someone else can.

like image 20
Nic Gibson Avatar answered Jan 30 '26 20:01

Nic Gibson