1

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;
Rowe Morehouse
  • 4,451
  • 3
  • 28
  • 28
  • What does it do when you run it at the command line? – Jim Davis May 21 '20 at 04:58
  • `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.` – Rowe Morehouse May 21 '20 at 05:03
  • 1
    Your perl installation is missing module `CGI`. It can be installed with `cpan CGI` and then try to run the script once more. – Polar Bear May 21 '20 at 08:13
  • Excellent, @PolarBear. Thank you … if you want to answer the question, then I will mark it as accepted! … thanks again. I had no idea how to work with perl on OSX. – Rowe Morehouse May 21 '20 at 09:48
  • 1
    On CGI https://stackoverflow.com/q/1162029/1216776 – stark May 22 '20 at 18:08

1 Answers1

3

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.

brian d foy
  • 129,424
  • 31
  • 207
  • 592