[Wellington-pm] 'enum' for Perl?
Cliff Pratt
enkidu at cliffp.com
Sun Apr 16 17:36:21 PDT 2006
Cliff Pratt wrote:
> Grant McLean wrote:
>
>>On Sun, 2006-04-16 at 20:20 +1200, Cliff Pratt wrote:
>>
>>
>>>Is there a way of doing enums in Perl?
>>>
>>>eg so that using the string ONE in a program has the same effect as using 1.
>>>
>>>exit ONE ; # exit 1
>>
>>
>>You could say:
>>
>> use constant ONE => 1;
>>
>>Or you could simply use a variable:
>>
>> my $ONE = 1;
>>
>>Possibly with the addition of the Readonly module from CPAN:
>>
>> Readonly my $ONE => 1;
>>
>>When you have a number of possible values that you want to assign
>>symbolic names to, variables are easiest, eg:
>>
>> my($IDLE, $WAITING, $CONNECTED, $RECEIVING, $DISCONNECTING) = (1..10);
>>
>>In that example, I declared an overly large range on the right of the
>>assignment, so that I could easily add new states to the left as I
>>needed them.
>>
>
> Thanks Grant,
>
> I can see how that works, but some modules export what appear to be
> variables, eg
>
> use Glib qw(TRUE FALSE) ;
>
> and you can then say, eg
>
> my $debug = FALSE ;
>
> Or is FALSE just a hidden subroutine that sets the return to the
> appropriate value?
>
To answer my question, Glib uses the Exporter module and 'use constant'.
I don't yet understand *what* it is doing exactly, but that is what is
*does* do. <grin>
Cheers,
Cliff
--
http://barzoomian.blogspot.com
More information about the Wellington-pm
mailing list