从CGI脚本中的文件读取的反向数组行

时间:2014-10-28 19:18:59

标签: perl cgi

我有一个我编写的Perl脚本,将其保存为index.cgi,它在一个短链接表中读取并从TSV中扩展:

redirects.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>&rarr;</td><td style='padding-left:15px'>$2</td></tr>\n";
}

print <<HTML;
<html>
<head>
  <link href="/favicon.png" rel="icon">
  <title>tblh.mn &rarr; tobilehman.com</title>
</head>
<body>
  <h1>Current Time: $current_time</h1>
  <h1>Short Links</h1>
  <table>
        $redirect_html
  </table>
</body>
</html>

HTML
exit;

目前,这些链接的打印方式与Last-In-Last-Out相同,但我希望它们可以打印为Last-In-First-Out。

我尝试使用reverse(<$lines>)<reverse($lines)>来解决这个问题。我对Perl不是很熟悉,如果这是一个菜鸟问题,请原谅我。

2 个答案:

答案 0 :(得分:2)

正如已经指出的那样,您可以使用reverse来反转数组,或File::ReadBackwards来执行其名称所暗示的内容。

此外,我还是鼓励您在脚本中进行一些基本的错误检查:

  1. 始终在每个脚本中加入use strict;use warnings;
  2. 在您进行文件处理时,请包括use autodie
  3. 在使用捕获变量之前,请确保您的正则表达式匹配。
  4. 风格上:

    1. 使用连接运算符构建字符串$string .= "more string";
    2. 如果要在字符串中包含双引号,请使用qq{ }之类的替代分隔符。
    3. 将这些更改包含在其他几个小修补程序中:

      #!/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>&rarr;</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 &rarr; tobilehman.com</title>
      </head>
      <body>
        <h1>Current Time: $current_time</h1>
        <h1>Short Links</h1>
        <table>
              $redirect_html
        </table>
      </body>
      </html>
      
      HTML
      exit;
      

答案 1 :(得分:1)

for my $line (reverse(<$lines>)) {
   ...
}

或者,File::ReadBackwards