Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Perl have an enumeration type?

Tags:

enums

perl

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

The project I am working one uses strings all over the place to denote things that would typically use an Enum in a language like C#. For example, we have a set of phone numbers in an array of hashes, each associated with a phone type ("Home", "Work", "Mobile", etc.):

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

Would it be sufficient to use a read-only set of variables here or should an Enum be used? I've found an enum module on CPAN but it appears to use bare words which violates one of the Perl Best Practices. My thinking on using read-only variables goes 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?

like image 507
cowgod Avatar asked Jan 23 '09 16:01

cowgod


People also ask

What is the example of enumerated data type?

An enumerated type is a type whose legal values consist of a fixed set of constants. Common examples include compass directions, which take the values North, South, East and West and days of the week, which take the values Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday.

What is the enumerator data type?

An enumeration is a data type that consists of a set of named values that represent integral constants, known as enumeration constants. An enumeration is also referred to as an enumerated type because you must list (enumerate) each of the values in creating a name for each of them.

Can enum be a type?

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it.

How do you declare an enumeration?

An enum is defined using the enum keyword, directly inside a namespace, class, or structure. All the constant names can be declared inside the curly brackets and separated by a comma. The following defines an enum for the weekdays. Above, the WeekDays enum declares members in each line separated by a comma.


1 Answers

No, there isn't a built-in enum construct. Perl doesn't do a lot of strict typing, so I think there's actually little need for one.

In my opinion, the Readonly approach you used is solid.

There's also the more traditional constant pragma.

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

Behind the scenes, it sets up a function for each constant that returns the value, like so.

sub HOME () { 'Home' } 

I'd stick with Readonly unless you want to take advantage of that property, for example:

package Phone::Type;  use constant {     HOME => 'Home',     #... };  package main;  print Phone::Type->HOME, "\n"; 
like image 119
Ronald Blaschke Avatar answered Oct 22 '22 13:10

Ronald Blaschke