gpt4 book ai didi

perl - 动态变量值?

转载 作者:行者123 更新时间:2023-12-04 14:00:26 25 4
gpt4 key购买 nike

代码:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com