如何在 Perl 中创建枚举类型?
我需要在 perl 中传回一个枚举值,我该怎么做?
从这个线程中提取: Perl 有枚举类型吗?
use strict;
use constant {
HOME => 'home',
WORK => 'work',
MOBILE => 'mobile',
};
my $phone_number->{type} = HOME;
print "Enum: ".$phone_number->{type}."\n";
但不应该这个返回索引0?或者我理解错了?
编辑:
那么对于枚举类型来说,这样的事情会更令人期待吗?
use strict;
use constant {
HOME => 0,
WORK => 1,
MOBILE => 2,
};
my $phone_number->{type} = HOME;
print "Enum: ".$phone_number->{type}."\n";
编辑#2
另外,我想验证所选的选项,但传回单词而不是值。我怎样才能充分利用这两个例子呢?
@VALUES = (undef, "home", "work", "mobile");
sub setValue {
if (@_ == 1) {
# we're being set
my $var = shift;
# validate the argument
my $success = _validate_constant($var, \@VALUES);
if ($success == 1) {
print "Yeah\n";
} else {
die "You must set a value to one of the following: " . join(", ", @VALUES) . "\n";
}
}
}
sub _validate_constant {
# first argument is constant
my $var = shift();
# second argument is reference to array
my @opts = @{ shift() };
my $success = 0;
foreach my $opt (@opts) {
# return true
return 1 if (defined($var) && defined($opt) && $var eq $opt);
}
# return false
return 0;
}
I need to pass back a enum value in perl, how can I do this?
pulling from this thread: Does Perl have an enumeration type?
use strict;
use constant {
HOME => 'home',
WORK => 'work',
MOBILE => 'mobile',
};
my $phone_number->{type} = HOME;
print "Enum: ".$phone_number->{type}."\n";
but shouldn't this return index 0? or am I understanding this wrong?
EDIT:
So would something like this be more expectable for a enum type?
use strict;
use constant {
HOME => 0,
WORK => 1,
MOBILE => 2,
};
my $phone_number->{type} = HOME;
print "Enum: ".$phone_number->{type}."\n";
EDIT #2
Also I would like to validate on the option selected but pass back the Word rather then the Value. How can I have the best of both examples?
@VALUES = (undef, "home", "work", "mobile");
sub setValue {
if (@_ == 1) {
# we're being set
my $var = shift;
# validate the argument
my $success = _validate_constant($var, \@VALUES);
if ($success == 1) {
print "Yeah\n";
} else {
die "You must set a value to one of the following: " . join(", ", @VALUES) . "\n";
}
}
}
sub _validate_constant {
# first argument is constant
my $var = shift();
# second argument is reference to array
my @opts = @{ shift() };
my $success = 0;
foreach my $opt (@opts) {
# return true
return 1 if (defined($var) && defined($opt) && $var eq $opt);
}
# return false
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
常量不是枚举(在 Perl 或我所知道的任何语言中)
不,因为这里您要做的是在符号表中插入键
HOME
和文字Home
,这在 Perl 术语中也称为bareword
。符号表是用散列实现的,其键和它们添加的顺序没有数字等价性。在您的示例中,您要做的是设置
$perl_number->{type} = 'Home'
,然后打印$phone_number->{type}
。A constant is not an enum (in perl, or any language I know of)
No, because here what you're doing is inserting in the symbol table a link between the key
HOME
and the literalHome
, this is also called abareword
in perl parlance. The symbol table is implemented with a hash, and there is no number equivalence of its keys and the order they were added.In your example what you're doing is setting
$perl_number->{type} = 'Home'
, and then printing out$phone_number->{type}
.如果您需要枚举,请使用 enum 模块。
If you want enums, use the enum module.