Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Enum

by Anonymous Monk
on Jun 20, 2000 at 11:54 UTC ( [id://18948]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Is there something similiar to a C/C++ enum? kn

Replies are listed 'Best First'.
Re: Enum
by t0mas (Priest) on Jun 20, 2000 at 14:55 UTC
    There is a enum module at CPAN, maybe it's worth a try.

    /brother t0mas
RE: Enum
by Russ (Deacon) on Jun 20, 2000 at 12:17 UTC
    Hashes could do the trick:
    my %enum = (Russ => 1, Anonymous Monk => 2, vroom => 3); $enum{Russ}; # value is 1 $enum{Anonymous Monk}; # value is 2 $enum{vroom}; # value is 3
    Update I suppose I should reverse my hash to make it work like enums are often used:
    my %enum = (1 => 'Russ', 2 => 'Anonymous Monk'); # Imagining a switch statement (but not wanting to show # a perl-ified way to do that...) if ($enum{$AnotherVar} eq 'Russ'){ doSomething(); }
    I've forgotten most of my C mentality (as fast as I could) ;-)

    Russ

Re: Enum
by ar0n (Priest) on Jun 20, 2000 at 14:12 UTC
    you could make a sub for each enum-type:
    sub BLUE { 1 } sub RED { 2 } sub GREEN { 3 } ... print "Green" if $color == GREEN; print "Blue" if $color == BLUE;
    it's not very practical if you have alot of types,
    but just a few would work.

    -- ar0n
      There is also a 'constant' module for defining constants in a similar (and perhaps more readable) fashion. Set them to powers of 2 and you can even use AND/OR operations on variables using them.
RE: Enum
by Q*bert (Sexton) on Jun 20, 2000 at 23:23 UTC
    They're not really necessary most of the time, since you can just set a scalar to a string without having to worry about giving it a mnemonic value: For instance, instead of defining an enum and then writing
    my $type_of_fruit = enum->{'ORANGE'};
    you can just go $my type_of_fruit = 'orange';. This kind of logic is the most common use of enums I've seen in C.

    Of course, numerical comparisons will be faster than string comparisons, so if you are examining many such values (e.g. in an extended chain of elsif's) then a C-like enum might be useful. What were you planning to do with your enum? That's probably the right question to ask.

    Just my $.02. Perl is different from C. It may not make sense to use the same idioms you learned in C.

Re: Enum
by ase (Monk) on Jun 21, 2000 at 05:46 UTC
    On page 208 of the original Camel (1st edition), There is a subroutine that I think will do what you want:
    sub enum { local($_)=@_; my(@specs)=split(/,/); #changed from local to my as it is preferab +le since perl 5. my $val; for (@specs) { if (/=/) { $val = eval $_; } else { eval $_ . ' = ++$val'; } } }
    An example of use is given:
    &enum(<<'EOL'); $RED, $GREEN, $BLUE, $CYAN='a', $MAGENTA, $YELLOW, $BLACK=-1 EOL print "$RED $GREEN $BLUE $CYAN $MAGENTA $YELLOW $BLACK\n";
    would print: 1 2 3 a b c -1
    I wonder who wrote this originally, Randal or Larry? Care to tell us merlyn?
    -ase
RE: Enum
by Q*bert (Sexton) on Jun 20, 2000 at 23:23 UTC
    They're not really necessary most of the time, since you can just set a scalar to a string without having to worry about giving it a mnemonic value: For instance, instead of defining an enum and then writing Of course, numerical comparisons will be faster than string comparisons, so if you are examining many such values (e.g. in an extended chain of elsif's) then
Re: Enum
by mdillon (Priest) on Jun 20, 2000 at 12:24 UTC
    redacted! (i got confused when i read the question)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://18948]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-19 13:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found