Perl CGI脚本中从文件读取的行的反向数组
我编写了一个Perl脚本,将其保存为Perl CGI脚本中从文件读取的行的反向数组,perl,cgi,Perl,Cgi,我编写了一个Perl脚本,将其保存为index.cgi,它从TSV中读取短链接及其扩展: 重定向.tsv: $ head redirects.tsv about http://tobilehman.com/about gapminder http://www.gapminder.org/ speed http://speedof.me/ delete http://justdelete.me/ terms http://tosdr.org/ re https://www.de
index.cgi
,它从TSV中读取短链接及其扩展:
重定向.tsv:
$ head redirects.tsv
about http://tobilehman.com/about
gapminder http://www.gapminder.org/
speed http://speedof.me/
delete http://justdelete.me/
terms http://tosdr.org/
re https://www.debuggex.com/
1 http://www.diffen.com/difference/Diffen_vs_Wikipedia
2 http://arstechnica.com/information-technology/2013/10/google-fiber-now-explicitly-permits-home-servers/
3 https://www.senate.gov/legislative/LIS/roll_call_lists/roll_call_vote_cfm.cfm?congress=113
ifs http://tobilehman.com/blog/2013/10/19/revisiting-spaces-in-file-names/
index.cgi:
#!/usr/bin/perl
print "Content-type: text/html\n\n";
my $current_time = time();
my $file = "redirects.tsv";
open my $lines, $file or die "Could not open redirects.tsv";
my $redirect_html = "";
while ( my $line = <$lines> ) {
$line =~ /^([0-9a-z_-]+)\t(.*)/;
#$redirect_html = "$redirect_html\n<li><a href='$1'>tblh.mn/$1</a> ($2)</li>";
my $link = "http://tblh.mn/$1";
$redirect_html
= "$redirect_html<tr><td><a href='$link'>$link</td><td>→</td><td style='padding-left:15px'>$2</td></tr>\n";
}
print <<HTML;
<html>
<head>
<link href="/favicon.png" rel="icon">
<title>tblh.mn → tobilehman.com</title>
</head>
<body>
<h1>Current Time: $current_time</h1>
<h1>Short Links</h1>
<table>
$redirect_html
</table>
</body>
</html>
HTML
exit;
#/usr/bin/perl
打印“内容类型:text/html\n\n”;
我的$current_time=time();
my$file=“redirects.tsv”;
打开我的$lines、$file或die“无法打开重定向.tsv”;
我的$redirect_html=“”;
while(我的$line=){
$line=~/^([0-9a-z_-]+)\t(.*)/;
#$redirect_html=“$redirect_html\n($2) ”;
我的$link=”http://tblh.mn/$1";
$redirect\u html
=“$redirect\u html$link&rarr;$2\n”;
}
为我的$line打印(反向()){
...
}
或者,.正如已经指出的,您可以使用来反转数组,或者执行其名称所暗示的操作
此外,我鼓励您在脚本中执行一些基本的错误检查:
在每个脚本中始终包含和
包括您进行文件处理的任何时间
在使用捕获变量之前,请确保正则表达式匹配
在风格上:
使用串联运算符生成字符串$string.=“更多字符串”代码>
每当您想在字符串中包含双引号时,请使用诸如qq{}
之类的可选分隔符
包括这些更改和几个其他小修补程序:
#!/usr/bin/perl
use strict;
use warnings;
use autodie;
print "Content-type: text/html\n\n";
my $current_time = time();
my $file = "redirects.tsv";
open my $fh, '<', $file;
my $redirect_html = "";
for ( reverse <$fh> ) {
chomp;
if ( my ( $shortlink, $full ) = /^([0-9a-z_-]+)\t(.*)/ ) {
my $link = "http://tblh.mn/$shortlink";
$redirect_html
.= qq{<tr><td><a href="$link">$link</td><td>→</td><td style="padding-left:15px">$full</td></tr>\n};
#$redirect_html .= "\n<li><a href='$1'>tblh.mn/$1</a> ($2)</li>";
}
}
print <<"HTML";
<html>
<head>
<link href="/favicon.png" rel="icon">
<title>tblh.mn → tobilehman.com</title>
</head>
<body>
<h1>Current Time: $current_time</h1>
<h1>Short Links</h1>
<table>
$redirect_html
</table>
</body>
</html>
HTML
exit;
#/usr/bin/perl
严格使用;
使用警告;
使用自动模具;
打印“内容类型:text/html\n\n”;
我的$current_time=time();
my$file=“redirects.tsv”;
打开我的$fh,'在风格上,为什么串联优于插值?简单。通过查看串联运算符=
,可以立即看到您的意图。很明显,您正在构建一个字符串,因此可以立即查找以确保my
声明在循环之外。这是有争议的,在我看来,用一行代码显示整个字符串更简单。不需要很长时间就可以看到里面的$符号,看到哪些是可变的,哪些不是。当然,你可以有自己的偏好。然而,还有两个好处。1) 它省去了打字。在本例中,1个字符对14个字符。2) 将实现方法从构建输出字符串更改为直接打印并非罕见。使用更简单的格式,只需删除开头并用打印替换即可。对于后者,我们必须认识到字符串中也有一个需要删除的变量。这不是什么大问题,显然可以解决。然而,为什么不直接使用simpler,因为它既节省了时间,也可能节省了将来的时间?
#!/usr/bin/perl
use strict;
use warnings;
use autodie;
print "Content-type: text/html\n\n";
my $current_time = time();
my $file = "redirects.tsv";
open my $fh, '<', $file;
my $redirect_html = "";
for ( reverse <$fh> ) {
chomp;
if ( my ( $shortlink, $full ) = /^([0-9a-z_-]+)\t(.*)/ ) {
my $link = "http://tblh.mn/$shortlink";
$redirect_html
.= qq{<tr><td><a href="$link">$link</td><td>→</td><td style="padding-left:15px">$full</td></tr>\n};
#$redirect_html .= "\n<li><a href='$1'>tblh.mn/$1</a> ($2)</li>";
}
}
print <<"HTML";
<html>
<head>
<link href="/favicon.png" rel="icon">
<title>tblh.mn → tobilehman.com</title>
</head>
<body>
<h1>Current Time: $current_time</h1>
<h1>Short Links</h1>
<table>
$redirect_html
</table>
</body>
</html>
HTML
exit;