The value of a dynamic variable?

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 values ​​each time? (do int(rand(10)) every time $config{ randValue } )

+6
source share
1 answer

You can use a related hash or function:

 my %config = ( randValue => sub { int(rand(10)) }, ); print $config{randValue}->(); print $config{randValue}->(); 
+10
source

Source: https://habr.com/ru/post/970946/


All Articles