Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic variable value?

Tags:

perl

perl code:

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

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

will produce:

8
8

Is it possible to get different value each time? (execute int(rand(10)) each time the $config{ randValue } is called)

like image 342
user3727573 Avatar asked Jun 17 '14 12:06

user3727573


1 Answers

You can either use tied hash, or function:

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

print $config{randValue}->();
print $config{randValue}->();
like image 196
mpapec Avatar answered Sep 28 '22 17:09

mpapec