Perl使用错误

时间:2014-01-30 20:57:30

标签: perl

任何人都可以告诉我为什么我会这样:

usage: gen-non-random.pl <count> <outputfile>

从下面的代码:     #!/ usr / bin / perl -w     #     #脚本生成非随机值,以演示错误的随机性图     #为我的“Howto Analyze SessionIDs”。     #     #撰写:

$version = "0.0.4";
$filename = "gen-non-random.pl";
$usage = "usage: $filename <count> <outputfile>\n";

$count = $ARGV[0] or die ("$usage\n");
$output = $ARGV[1] or die ("$usage\n");
print ("-- $filename Version: $version\n");

use Time::HiRes qw( usleep ualarm gettimeofday tv_interval );
use Math::Random;
use Digest::MD5 qw(md5_hex);

open (OUT, ">$output") or die ("Can't open $output\n");

for ($i=0; $i<$count;$i++)
{
# generate a random number
$random = random_uniform();
# cut out char 3-9 of $random and put it in $randsub
$randsub = substr($random, 2, 6);
# get seconds and microseconds since epoch
($seconds, $microseconds) = gettimeofday;
# get the last two chars of the seconds and put them into $s
$s = substr($seconds, 8, 2);
# sleep for a while
usleep $randsub;
# put together the last two digits of seconds and the microseconds
$time = $s . $microseconds;
$md5_time=md5_hex($time);
# print out the stuff we put together above
print OUT ("$md5_time\n");
}
close (OUT) or die ("Can't close $output\n");
print ("$count values written to $output\n");
exit;

我是编程新手,所以我需要非常简单的答案!我没有拥有我在大学的研究论文中使用的代码。另外,有人可以向我解释一下,实际上我的用法似乎无法找到一个好的解释吗? 感谢。

2 个答案:

答案 0 :(得分:2)

您收到该错误是因为您没有正确使用程序:

usage: gen-non-random.pl <count> <outputfile>

这基本上意味着您必须提供计数和输出文件作为参数,例如:

perl gen-non-random.pl 42 outfile.txt

这将生成四十二个数字并将它们输出到outfile.txt文件。

这是开始附近的两行,检查ARGV[0/1]die - 如果您不提供它们,则输出此消息并退出程序。

答案 1 :(得分:0)

嗯。我无法运行上面的代码,因为在Windows上没有实现Time :: HiRes :: ualarm()。也就是说,它似乎是在睡眠一段随机秒后生成MD5的当前时间字符串(整数形式),然后将结果转储到文本文件中。您正在获取上面提到的用法消息,因为程序需要输入。尝试从命令行运行它,如下所示:

perl gen-non-random.pl 10 MyResults.txt

我怀疑会将10个HD5哈希结果转储到名为“MyResults.txt”的文件中。

相关问题