I would like to display .html files in green, .jpg files in blue and so on..
Building on the last example, I will use a hash table to store the color associated with the extension and the File::Basename module to get the extension and apply the color.
use File::Basename;
$dir = '/home/username/public_html/';
$DirName = 'images';
my %colors = (
'.html' => 'green',
'.jpg' => 'blue',
'.gif' => 'blue',
'.txt' => 'red',
);
doit();
sub doit {
opendir(DIRHANDLE,"$dir$DirName") or die "Can't open $dir$DirName: $!";
@filenames = sort readdir(DIRHANDLE);
close(DIRHANDLE);
foreach $dirfile (@filenames) {
my(undef, undef, $ftype) = fileparse($dirfile,qr{\..*});
<strong>my $color = $colors{lc($ftype)} || 'black';</strong>
print qq~<span style="color:$color">$dirfile</span>\n~;
}
}
** apply your own HTML code to the output that gets printed, mine is just for the purpose of the