Perl Source Code: images.pl
#!/usr/bin/perl
use CGI qw(:standard);
use common;
#----------------------------------------------------------
# The Web Language Project
# Mark Brautigam
# May-June 2015
# http://www.mixed-up.com/markb/
#----------------------------------------------------------
myUtils::headers1();
myUtils::headers2();
print myUtils::sidebar ("perl", "images", "pl");
print <<middle;
<div id="content">
<h2>Perl: Directory Read</h2>
<p>Read the contents of a directory of images. Exclude the non-image directory entries.
Create a caption by deleting the file extension and capitalizing the file name.</p>
<p class='choose'>Choose a directory:
<a href='images.pl?dir=comics'>Comics</a> •
<a href='images.pl?dir=embroidery'>Embroidery</a> •
<a href='images.pl?dir=stone'>Carving</a> •
<a href='images.pl?dir=tech'>Tech</a>
</p>
middle
#----------------------------------------------------
# The main Perl code is here.
#----------------------------------------------------
my $dir = "../images/embroidery";
if (param('dir')) {
$dir = "../images/" . param('dir');
}
opendir (DIR, $dir);
my @images = readdir (DIR);
closedir (DIR);
my $nimages = @images;
for (my $i=0; $i<$nimages; $i++) {
if (substr ($images[$i], 0, 1) ne '.' && substr($images[$i], 0, 1) ne '_') {
my $caption = makeCaption ($images[$i]);
print ( "<figure>\n");
print ( " <img src='$dir/$images[$i]' />\n");
print ( " <figcaption>$caption</figcaption>\n");
print ( "</figure>\n");
}
}
print <<foot;
<p>We can change the order of the files (for example, birth order instead of alphabetical
order) by prepending each file name with a number and removing the number from the
caption string.</p>
<p>Images courtesy of:<br />
<a target='_blank'
href='http://www.worldofjudaica.com/jewish-home/stationery/p_jerusalem_stone_paperweight_with_twelve_tribes_and_menorah'>
Stone Paperweight at World of Judaica</a><br />
<a target='_blank'
href='http://4-hobby.com/store/product.php?productid=17686&cat=320&page=1'>
Embroidery Designs at 4-Hobby.com</a><br />
<a target='_blank' href='http://www.dumbingofage.com'>Dumbing of Age</a><br />
<a target='_blank' href='http://www.scarygoround.com'>Scary Go Round</a><br />
<a target='_blank' href='http://www.questionablecontent.net'>Questionable Content</a>
</p>
<p><a href='source.pl?f=5'>Show Perl source code »</a>
</div>
foot
myUtils::footers();
#----------------------------------------------------
sub makeCaption
#----------------------------------------------------
{
my $caption = ucfirst (substr ($_[0], 0, -4));
if (index ($caption, "_") > -1) {
@captions = split ("_", $caption);
for (my $i=0; $i<@captions; $i++) {
$captions[$i] = ucfirst ($captions[$i]);
}
$caption = join ("<br />", splice (@captions, 1));
}
}