如何在 Perl 中创建枚举类型?

发布于 2024-08-19 03:37:06 字数 1403 浏览 5 评论 0原文

我需要在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

煮酒 2024-08-26 03:37:06

常量不是枚举(在 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 literal Home, this is also called a bareword 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}.

逆蝶 2024-08-26 03:37:06

如果您需要枚举,请使用 enum 模块。

If you want enums, use the enum module.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文