I've revisited this script and made some enhancements and cleaned it up so it is less obfuscated. (-:
#!/usr/bin/perl -w
use strict;
use CGI qw/:standard/;
$|=1;
my $columns = param('c') || 4;
opendir(DIR, ".") || die "cannot open dir: $!";
foreach (sort readdir(DIR)) {
next if /~\z/ || $_ eq "ls.cgi" || $_ eq "." || $_ eq "..";
push @{ (-d) ? $_[0] : $_[1] },
(-d) ? b(a({href=>"$_"},"[ $_ ]")) : a({href=>"$_"},"$_");
}
closedir DIR;
# Combine dirs and files
$_[3] = [ @{$_[0]}, @{$_[1]} ];
print
header,
start_html('Directory Listing'),
h3('Directory Listing'),
b("Dir Count: ".@{$_[0]}), b(", File Count: ".@{$_[1]}),
table({-border=>0,-cellpadding=>5,-cellspacing=>5},
map Tr($_),
grep { length($_) > 0 }
map td([ splice @{$_[3]},0,$columns ]), @{$_[3]}
),
end_html;
__END__
Most notably I've taken another look at Aristotle's code and replaced the cute splice with array refs which turns out to be useful in the output section. E.g. I've added a directory count and a file count.
The other noticable part is that I've removed the 0 .. (@_ / $columns) and inserted a grep to eliminate extra list elements that cause extra TR elements (the initial reason I put that in there in the first place).
And, yes, I cleaned up the exclusion logic to use eq, etc.
Enjoy!
Update: Yes, the grep is a hack, for a full treatize on this issue click here.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.