如何从终端在Mac上运行此简单的Perl CGI脚本? [英] How to run this simple Perl CGI script on Mac from terminal?

查看:127
本文介绍了如何从终端在Mac上运行此简单的Perl CGI脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This simple .pl script is supposed to grab all of the images in a directory and output an HTML — that when opened in a browser — displays all of the images in that dir at their natural dimensions.

在mac命令行中,我只想说 perl myscript.pl 并使其运行.

From the mac command line, I want to just say perl myscript.pl and have it run.

...它曾经在/cgi-bin 中的apache上运行.

… It used to run on apache in /cgi-bin.

#!/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;

推荐答案

Perl曾经在标准库中包括CGI模块,但是在v5.22中已将其删除(请参见

Perl used to include the CGI module in the Standard Library, but it was removed in v5.22 (see The Long Death of CGI.pm). Lots of older code assumed that it would always be there, but now you have to install it yourself:

$ cpan CGI

Perl曾经在标准库中包含CGI模块,但是在v5.22中已将其删除.许多以前的代码都假定它会一直存在,但是现在您必须自己安装它.

Perl used to include the CGI module in the Standard Library, but it was removed in v5.22. Lots of older code assumed that it would always be there, but now you have to install it yourself.

Perl随附的 corelist 程序可方便地检查以下内容:

The corelist program that comes with Perl is handy for checking these things:

$ 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

我通过使用 extract_modules处理此类事情/a>程序从我的 Module :: Extract :: Use 模块中.否则,我最终将安装一个模块,然后再次运行并发现另一个要安装的模块,依此类推:

I handle this sort of thing by using the extract_modules program from my Module::Extract::Use module. Otherwise, I end up installing one module, then run again and discover another one to install, and so on:

$ extract_modules some_script.pl | xargs cpan

模块编写者还有一个有趣的观点.长期以来,我们只会在 Makefile.PL 中列出外部先决条件.现在,Perl拥有将模块踢出标准库的先例,您甚至应该列出内部组件.除此之外,请为您实际使用的任何模块指定一个依赖关系,而不要依赖于特定发行版中的依赖关系.

There's another interesting point for module writers. For a long time, we'd only list the external prerequisites in Makefile.PL. You should list even the internal ones now that Perl has a precedent for kicking modules out of the Standard Library. Along with that, specify a dependency for any module you actually use rather than relying it being in a particular distribution.

而且,我在遗留程序方面做了大量的工作,以至于我写了一个小工具 scriptdist 将模块基础结构包装在单文件程序周围,以便我可以将它们安装为模块.最大的好处是 cpan 和类似的工具为您安装了先决条件.自从我现在开始以常规Perl发行版形式启动程序以来,我已经很长时间没有使用它了.

And, I was moving legacy programs around so much that I wrote a small tool, scriptdist to wrap the module infrastructure around single-file programs so I could install them as modules. The big win there is that cpan and similar tools install the prereqs for you. I haven't used it in a long time since I now just start programs as regular Perl distributions.

这篇关于如何从终端在Mac上运行此简单的Perl CGI脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆