Category: Script Keywords: random link
一个简单的脚本,功能就是获得随机的一篇 journal. :)
仅供参考。
#!/usr/bin/perl
use CGI qw/redirct/; # 获取 redirect 方法用于输出 302 转网页
my $dir = "$ENV{'DOCUMENT_ROOT'}/journal"; # 网页所在的绝对路径
unless (-e $dir) { die "no such dir, please set the \$dir\n$dir non-exist"; } # 检测该路径是否存在
opendir(DIR, $dir);
my @files = readdir(DIR); # 获取所有文件
closedir(DIR);
my @wantfiles;
foreach (@files) {
next unless (/\.html$/); # 我们只需要 HTML 文件
next if (/^index/); # 忽略所有 index 开头的索引文件
push(@wantfiles, $_);
}
my $total = scalar @wantfiles; # 文件的个数
my $random = int(rand($total)); # 由此获得的随机数字
print CGI::redirect("http://www.fayland.org/journal/$wantfiles[$random]"); # 转网页地址
It's a easy hack, and have fun!