如何在 Perl 中创建嵌套哈希作为常量?

发布于 2024-07-30 01:18:35 字数 356 浏览 2 评论 0原文

我想要在 Perl 中执行与以下 Ruby 代码等效的操作:

class Foo
  MY_CONST = {
    'foo' => 'bar',
    'baz' => {
      'innerbar' => 'bleh'
    },
  }

  def some_method
    a = MY_CONST[ 'foo' ]
  end

end

# In some other file which uses Foo...

b = Foo::MY_CONST[ 'baz' ][ 'innerbar' ]

也就是说,我只想声明一个常量、嵌套哈希结构,以便在类中和外部使用。 如何?

I want to do, in Perl, the equivalent of the following Ruby code:

class Foo
  MY_CONST = {
    'foo' => 'bar',
    'baz' => {
      'innerbar' => 'bleh'
    },
  }

  def some_method
    a = MY_CONST[ 'foo' ]
  end

end

# In some other file which uses Foo...

b = Foo::MY_CONST[ 'baz' ][ 'innerbar' ]

That is, I just want to declare a constant, nested hash structure for use both in the class and outside. How to?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

甜味拾荒者 2024-08-06 01:18:35

您也可以完全使用内置函数来完成此操作:

package Foo;
use constant MY_CONST =>
{
    'foo' => 'bar',
    'baz' => {
        'innerbar' => 'bleh',
    },
};

sub some_method
{
    # presumably $a is defined somewhere else...
    # or perhaps you mean to dereference a parameter passed in?
    # in that case, use ${$_[0]} = MY_CONST->{foo} and call some_method(\$var);
    $a = MY_CONST->{foo};
}

package Main;  # or any other namespace that isn't Foo...
# ...
my $b = Foo->MY_CONST->{baz}{innerbar};

You can also do this entirely with builtins:

package Foo;
use constant MY_CONST =>
{
    'foo' => 'bar',
    'baz' => {
        'innerbar' => 'bleh',
    },
};

sub some_method
{
    # presumably $a is defined somewhere else...
    # or perhaps you mean to dereference a parameter passed in?
    # in that case, use ${$_[0]} = MY_CONST->{foo} and call some_method(\$var);
    $a = MY_CONST->{foo};
}

package Main;  # or any other namespace that isn't Foo...
# ...
my $b = Foo->MY_CONST->{baz}{innerbar};
愁杀 2024-08-06 01:18:35

您可以使用 Hash::Util 模块来锁定和解锁哈希(键、值, 或两者)。

package Foo;
use Hash::Util;

our %MY_CONST = (
    foo => 'bar',
    baz => {
        innerbar => 'bleh',
    }
);

Hash::Util::lock_hash_recurse(%MY_CONST);

然后在其他一些文件中:

use Foo;
my $b = $Foo::MY_CONST{baz}{innerbar};

You can use the Hash::Util module to lock and unlock a hash (keys, values, or both).

package Foo;
use Hash::Util;

our %MY_CONST = (
    foo => 'bar',
    baz => {
        innerbar => 'bleh',
    }
);

Hash::Util::lock_hash_recurse(%MY_CONST);

Then in some other file:

use Foo;
my $b = $Foo::MY_CONST{baz}{innerbar};
紫竹語嫣☆ 2024-08-06 01:18:35

请参阅只读

#!/usr/bin/perl

package Foo;

use strict;
use warnings;

use Readonly;

Readonly::Hash our %h => (
    a => { b => 1 }
);

package main;

use strict;
use warnings;

print $Foo::h{a}->{b}, "\n";

$h{a}->{b} = 2;

输出:

C:\Temp> t
1
Modification of a read-only value attempted at C:\Temp\t.pl line 21

See Readonly:

#!/usr/bin/perl

package Foo;

use strict;
use warnings;

use Readonly;

Readonly::Hash our %h => (
    a => { b => 1 }
);

package main;

use strict;
use warnings;

print $Foo::h{a}->{b}, "\n";

$h{a}->{b} = 2;

Output:

C:\Temp> t
1
Modification of a read-only value attempted at C:\Temp\t.pl line 21
一腔孤↑勇 2024-08-06 01:18:35

这是 Perl 中哈希值的指南。 哈希的哈希

Here is a guide to hashes in perl. Hash of Hashes

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