我使用下面的函数为一次性下载链接(最初来自perlmonks)创建散列。奇怪的是,我总是得到相同的散列结果。
我已经被RTFMing了。我确保crypt()函数获得了$exp的最后8个字符,并且还验证了$exp确实发生了变化。我还尝试手动向crypt()函数提供随机值,只有这些值运行良好,散列结果才会发生变化。
这里我漏掉了什么?
use strict;
use CGI;
sub chash {
my $exp = $_;
my $key = 'abcd1234'; //not actual key
my $hash = crypt(substr($exp,-8,8),$key);
$hash = substr($hash, 2);
$hash =~ s/[^a-zA-Z0-9]//g; $hash = uc($hash);
return $hash;
}
my $exp = time() + 60;
my $hash = chash($exp);
my $download_url="http://script.pl?$exp-$hash";发布于 2014-04-22 17:18:05
您希望从@_中提取第一项,而不是尝试读取sub中的$_。
my $exp = shift;或
my ($exp) = @_;或
my $exp = $_[0];来自perlsub
传入的任何参数都会显示在数组@_中。因此,如果您使用两个参数调用函数,则这些参数将存储在$_[0]和$_[1]中。数组@_是一个本地数组,但它的元素是实际标量参数的别名。
发布于 2014-04-22 17:18:39
sub的参数将在@_中传递,而不是在$_中传递。
use strict;
use warnings ;
use CGI;
sub chash {
my ( $exp ) = @_;
my $key = 'abcd1234'; # not actual key
my $hash = crypt(substr($exp,-8,8),$key);
$hash = substr($hash, 2);
$hash =~ s/[^a-zA-Z0-9]//g;
$hash = uc($hash);
return $hash;
}
my $exp = time() + 60;
my $hash = chash($exp);
my $download_url="http://script.pl?$exp-$hash";使用use warnings;可能会提示您犯这个错误。
https://stackoverflow.com/questions/23215278
复制相似问题