Does Perl have an enum type?

Does Perl have an enum type that adheres to best practices or, more importantly, does it need it?

In the project I'm working in, all the lines are used to denote things that typically use Enum in C #. For example, we have a set of phone numbers in an array of hashes, each of which is associated with a phone type ("Home", "Work", "Mobile", etc.):

$phone_number->{type} = 'Home'; 

Is it enough to use read-only variables here or to use Enum? I found the enum module in CPAN , but it seems to use bare words that violate one of Perl Best Practices . My thinking about using read-only variables is something like this:

 use Readonly; Readonly my $HOME => 'Home'; Readonly my $WORK => 'Work'; Readonly my $MOBILE => 'Mobile'; $phone_number->{type} = $HOME; 

Is this a good approach or is there a better way?

+49
enums perl
Jan 23 '09 at 16:57
source share
7 answers

No, there is no built-in enum construct. Perl doesn't do a lot of strong typing, so I think that really a little is needed.

In my opinion, the Readonly used Readonly durable.

There is also a more traditional constant pragma.

 use constant { HOME => 'Home', WORK => 'Work', MOBILE => 'Mobile', }; $phone_number->{type} = HOME; 

Behind the scenes, he sets up a function for each constant that returns a value, for example.

 sub HOME () { 'Home' } 

I would stick with Readonly if you don't want to use this property, for example:

 package Phone::Type; use constant { HOME => 'Home', #... }; package main; print Phone::Type->HOME, "\n"; 
+38
Jan 23 '09 at 19:52
source share

Perl really has an enum type, like in C. Try this for details.

perldoc enum

For example:

 use enum qw(HOME WORK MOBILE); 

Now we have:

 HOME == 0 WORK == 1 MOBILE == 2 

You can also set indices yourself:

 use enum qw(HOME=0 WORK MOBILE=10 FAX); 

Now we have:

 HOME == 0 WORK == 1 MOBILE == 10 FAX == 11 

Check here for more details.

Please note that this is not supported in every version of Perl. I know that v5.8.3 does not support it, while v5.8.7 does.

+16
Jun 24 '09 at 11:17
source share

Perl does not support the concept natively, but there are modules to add this feature

https://metacpan.org/pod/enum

+10
Jan 23 '09 at 17:06
source share

Your path is more than adequate.

You can also create enums with Moose :: Util :: TypeConstraints if you use Moose. (Which should be.)

+8
Jan 23 '09 at 19:39
source share

I am afraid that perl is completely paraplegic when it comes to these enumerations and named constants:

  • enum and Readonly are not core modules (at least in perl 5.8, which I just checked). Thus, they may not be available in a system where there is no freedom in installing new modules.

  • "Use a constant" with "use strict" (which should always be used) is completely unusable as it generates a load of terrible form error messages:

    Cannot use the word "FRED" while using "strong subnets"

Let them hope they make out this mess in perl 6 if it ever sees the light of day!

+5
Mar 23 '11 at 15:41
source share

You should always remember that PBP is advisory - the book itself is the same. You need to interpret the guidelines, not slavishly accept them.

+2
Jun 24 '09 at 12:33
source share

It worked for me ...

 use constant MYENUM => qw(ZERO ONE TWO THREE FOUR); BEGIN { eval "use constant (MYENUM)[$_] => $_;" foreach 0..(MYENUM)-1; } 

Then you can use ZERO, ONE, TWO , etc. as constants and print your symbolic name using (MYENUM)[$value] .

0
Aug 26 '14 at 5:55
source share



All Articles