Perl is a web-friendly computing lanaguage that provides powerful text processing facilities to update web pages. CGI, or Common Gateway Interface, provides Perl apps with a protocol to communicate with web severs. This process allows for the implementation of dynamic and customized web pages. Below are some examples of Perl apps I have configured.
Number of files in a directory (Ex: Madagascar photos) #!/usr/bin/perl
use strict;
my %In = ();
my $NumFiles = 0;
my $JScode = '';
# 1. Read what the JavaScript sent.
($In{var},$In{loc}) = split /&/,$ENV{QUERY_STRING};
# 2. Grab a files listing from the directory
# 3. and count the number of files.
if(opendir D,$In{loc})
{
my @d = grep ! /^\.\.?$/,readdir D;
closedir D;
$NumFiles = @d;
}
# 4. Create JavaScript code to send back.
$JScode = "$In{var}=$NumFiles;";
# 5. Send code to the JavaScript on the web page.
print "Content-type: text/javascript\n\n";
print $JScode;
### end of script ###
Now make it work with JavaScript <script type="text/javascript" language="JavaScript">
<!--
NumberOfFiles = 0;
//-->
</script>
<script src="http://heybrian.com/cgi-bin/script.cgi?NumberOfFiles&/madagascar" type="text/javascript" language="JavaScript">
</script>
<script type="text/javascript" language="JavaScript">
<!--
document.write('<h2>' + NumberOfFiles + ' Photographs</h2>');
//-->
</script>