我正在尝试将库从 Javascript 移植到 Perl,但是在检测数组参数时遇到了一些问题。
一个精简的示例(在 Javascript 中)如下所示:
const say = console.log;
function test(array, integer)
{
if(Array.isArray(array))
{
say("First argument is array");
}
else
{
say("First argument is not array");
array = [array];
}
for(let index = 0; index < array.length; ++index)
{
say(array[index]);
}
say("Second argument: ", integer);
say("");
}
var a = ["foo", "bar"];
var t = "baz";
var i = 1024;
say("Testing on array");
test(a, i);
say("Testing on string");
test(t, i);
Run Code Online (Sandbox Code Playgroud)
输出:
Testing on array
First argument is …Run Code Online (Sandbox Code Playgroud) 我需要一个随机数发生器.不是伪随机数,而是真正的随机数.我想到,也许我可以从循环执行中的微妙时序差异中提取位,所以我把一些东西放在一起做到这一点:
#ifdef _WIN32
#include <windows.h>
unsigned long long tick()
{
unsigned __int64
tock;
QueryPerformanceCounter((LARGE_INTEGER *)&tock);
return (unsigned long long)tock;
}
#else
#include <time.h>
unsigned long long tick()
{
return (unsigned long long)clock();
}
#endif
#include <limits.h>
unsigned long long random_bits(unsigned short bits)
{
/*
The `threshold` setting determines the smallest sample to extract a bit
from. If set too low the result won't contain enough entropy to be useful.
We don't want to set it so high that …Run Code Online (Sandbox Code Playgroud)