Perl代码:

my %config = (
    randValue => int(rand(10)),
);

print $config{ randValue }."\n";
print $config{ randValue }."\n";

将产生:
8
8

每次都有可能获得不同的值(value)吗? (每次调用int(rand(10))时执行$config{ randValue })

最佳答案

您可以使用tied hash或函数:

my %config = (
    randValue => sub { int(rand(10)) },
);

print $config{randValue}->();
print $config{randValue}->();

关于perl - 动态变量值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24263363/

10-10 19:34