perl语言中的定义枚举

perl语言中定义枚举变量和C语言中类似,认都是从0开始,并依次递增1个单位;
也可以重新自定义枚举变量的起始值,示例如下:

定义枚举变量:

use enum qw(Sun Mon Tue Wed Thu Fri Sat);
# Sun == 0,Mon == 1,etc

use enum qw(Forty=40 FortyOne Five=5 Six Seven);
# Yes,you can change the start indexs at any time as in C

use enum qw(:Prefix_ One Two Three);
## Creates Prefix_One,Prefix_Two,Prefix_Three

use enum qw(:Letters_ A..Z);
## Creates Letters_A,Letters_B,Letters_C,...

use enum qw( :Months_=0 Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec :Days_=0 Sun Mon Tue Wed Thu Fri Sat );

测试代码

print "enum Wed val: ",Wed,"\n" ;
print "enum FortyOne val: ",FortyOne,"\n" ;
print "enum Prefix_Two val: ","\n" ;
print "enum Letters_A val: ",Letters_A,"\n" ;
print "enum Months_Jan val: ",Months_Jan,"\n" ;
print "enum Days_Sun val: ",Days_Sun,"\n" ;

测试结果:

enum Wed val: 3
enum FortyOne val: 41
enum Prefix_Two val: 1
enum Letters_A val: 0
enum Months_Jan val: 0
enum Days_Sun val: 0

更多参考资料:
https://metacpan.org/pod/enum

相关文章

1. 如何去重 #!/usr/bin/perl use strict; my %hash; while(...
最近写了一个perl脚本,实现的功能是将表格中其中两列的数据...
表的数据字典格式如下:如果手动写MySQL建表语句,确认麻烦,...
巡检类工作经常会出具日报,最近在原有日报的基础上又新增了...
在实际生产环境中,常常需要从后台日志中截取报文,报文的形...
最近写的一个perl程序,通过关键词匹配统计其出现的频率,让...