【问题标题】:How to run this simple Perl CGI script on Mac from terminal?如何从终端在 Mac 上运行这个简单的 Perl CGI 脚本?
【发布时间】:2020-09-07 15:56:03
【问题描述】:

这个简单的.pl 脚本应该抓取一个目录中的所有图像并输出一个 HTML(当在浏览器中打开时)以自然尺寸显示该目录中的所有图像。

在 mac 命令行中,我只想说 perl myscript.pl 并让它运行。

…它曾经在/cgi-bin的apache上运行。

#!/usr/bin/perl -wT
# myscript.pl

use strict;
use CGI;
use Image::Size;

my $q = new CGI;

my $imageDir = "./";
my @images;

opendir DIR, "$imageDir" or die "Can't open $imageDir $!";
    @images = grep { /\.(?:png|gif|jpg)$/i } readdir DIR;
closedir DIR;

print $q->header("text/html"),
      $q->start_html("Images in $imageDir"),
      $q->p("Here are all the images in $imageDir");

foreach my $image (@images) {
    my ($width, $height) = imgsize("$image");
    print $q->p(
            $q->a({-href=>$image},
              $q->img({-src=>$image,
                       -width=>$width,
                       -height=>$height})
            )
    );
}

print $q->end_html;

【问题讨论】:

  • 在命令行运行它会做什么?
  • Can't locate CGI.pm in @INC (you may need to install the CGI module) (@INC contains: /opt/local/lib/perl5/site_perl/5.24/darwin-thread-multi-2level /opt/local/lib/perl5/site_perl/5.24 /opt/local/lib/perl5/vendor_perl/5.24/darwin-thread-multi-2level /opt/local/lib/perl5/vendor_perl/5.24 /opt/local/lib/perl5/5.24/darwin-thread-multi-2level /opt/local/lib/perl5/5.24) at imagescrounge-2020.pl line 8.
  • 您的 perl 安装缺少模块 CGI。可以用cpan CGI安装,然后再次尝试运行脚本。
  • 太棒了,@PolarBear。谢谢……如果您想回答这个问题,那么我会将其标记为已接受! … 再次感谢。我不知道如何在 OSX 上使用 perl。

标签: perl cgi


【解决方案1】:

Perl 曾经在标准库中包含 CGI 模块,但在 v5.22 中被删除(请参阅 The Long Death of CGI.pm)。许多旧代码都假定它会一直存在,但现在您必须自己安装它:

$ cpan CGI

Perl 曾经在标准库中包含 CGI 模块,但在 v5.22 中被删除。许多旧代码假定它会一直存在,但现在您必须自己安装它。

Perl 附带的corelist 程序可以方便地检查这些内容:

$ corelist CGI

Data for 2020-03-07
CGI was first released with perl 5.004, deprecated (will be CPAN-only) in v5.19.7 and removed from v5.21.0

我使用Module::Extract::Use 模块中的extract_modules 程序来处理这类事情。否则,我最终会安装一个模块,然后再次运行并发现另一个要安装的模块,依此类推:

$ extract_modules some_script.pl | xargs cpan

对于模块编写者来说,还有另一个有趣的地方。长期以来,我们只会在 Makefile.PL 中列出外部先决条件。既然 Perl 有将模块踢出标准库的先例,您甚至应该列出内部的。除此之外,为您实际使用的任何模块指定一个依赖项,而不是依赖它在特定发行版中。

而且,我正在大量移动遗留程序,以至于我编写了一个小工具scriptdist 来将模块基础结构包装在单文件程序周围,以便我可以将它们安装为模块。最大的胜利是cpan 和类似的工具为您安装了先决条件。我已经很长时间没有使用它了,因为我现在只是将程序作为常规 Perl 发行版启动。

【讨论】:

    猜你喜欢
    • 2018-01-08
    • 2012-09-21
    • 1970-01-01
    • 2012-03-10
    • 2015-05-27
    • 1970-01-01
    • 2011-02-07
    • 1970-01-01
    相关资源
    最近更新 更多