How to run this simple Perl CGI script on Mac from terminal?

安稳与你 提交于 2021-01-28 13:36:02

问题


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.

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

… 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;

回答1:


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 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.

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

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

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.

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.



来源:https://stackoverflow.com/questions/61927403/how-to-run-this-simple-perl-cgi-script-on-mac-from-terminal

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!