#! perl.exe use CGI qw/:standard/; use strict; my $columns = param('cols') || 4; my $offset = 0; opendir(DIR, ".") || die "cannot opendir: $!"; foreach (sort readdir(DIR)) { next if /^\.\.?$/ || /~$/; my $d = -d "$_"; splice(@_, $d?$offset++:@_, 0, b($d?"[ ":"",a({-href=>"$_"},$_),$d +?" ]":"","\n")); } closedir DIR; print header(-expires=>'now'), start_html('Directory Listing'), h1('Directory Listing'), table({-border=>0,-cellpadding=>5,-cellspacing=>5}, map { Tr($_) } map { td( [ splice(@_,0,$columns) ] ) } 0 .. (@_ / $columns) ), end_html; __END__ # Here's an explanation of how the splice works: # # Tell @_ to pretend to be two arrays, @dirs and @files # and simulate a push command for both arrays. # In the splice documentation # <http://www.perldoc.com/perl5.6/pod/func/splice.html> # it shows sample code to simulate push: # # splice(@a,@a,0,$x,$y) # # Since we want to put the @dirs at the front and the # @files at the back, we first tried: # # unshift @_, $dirname # put the dirname first # push @_, $filename; # put the filename last # # that fails because now our directories are in reverse # order. In order to keep the ordering of the dir names, # there has to be a way to push to the middle of the # list. The solution here is to keep an offset into # the middle of the list where the new $dirname should # be inserted and increment the offset every time one # is added. For the case of a filename we revert to # the example above for a simulated push, or make the # offset point to the end of the list. # splice(@_, # this is our list $d?$offset++:@_, # if we have a dir, return # the current offset and # increment, otherwise # return length of list 0, # number of elements to replace # an html-ized version of the dir/file name # putting "[" and "]" around dirnames b($d?"[ ":"",a({-href=>"$_"},$_),$d?" ]":"","\n") );

In reply to Ordered Directory Listing CGI by hiseldl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.