You can use eval($str)to execute Perl code that you store in a string variable $str. You can also save your code as function references in a hash, so:
$hash{'random'} = sub { int(rand()) };
That way, you can write $hash{'random'}->()to execute a function whenever you want to get an arbitrary value.
See also Embedding send tables on PerlMonks.