nasa has asked for the wisdom of the Perl Monks concerning the following question:

I have a list of files in DIR start
Like so
12345.data
12346.data
12347.data
12348.data
12349.data
I finally have some code to print a list of the files
print "Content-type: text/html\n\n"; opendir(HOMEDIR, "\start") || die ("Unable to open directory"); while ($filename = readdir(HOMEDIR)) { print ("$filename\n"); } closedir(HOMEDIR);
This prints the list of files in the browser as

12345.data 12346.data 12347.data 12348.data 12349.data
What on earth must I do to this code to make the list appear in the browser as

12345.data
12346.data
12347.data
12348.data
12349.data
Tormenting little thing…. Nasa.

Replies are listed 'Best First'.
Re: directory printing
by ikegami (Patriarch) on Dec 10, 2006 at 05:42 UTC

    Alternatively, change
    print "Content-type: text/html\n\n";
    to
    print "Content-type: text/plain\n\n";

Re: directory printing
by spunk (Acolyte) on Dec 10, 2006 at 06:22 UTC
    Just a guess here...

    HTML does not recognize carrige returns (or any other whitespace) for document spacing. You need the HTML tag
    for this purpose. Within your while loop, add the following line...
    while ($filename = readdir(HOMEDIR)) { print ("$filename\n"); print ("<br>\n"); }
    Notice that it's not a bad idea keep your HTML source looking pretty by keeping those newline character in there. Hope that helps...
Re: directory printing
by McDarren (Abbot) on Dec 10, 2006 at 05:31 UTC
    This is not a Perl question, it is an HTML question.
    print "$filename<br>\n";
      You're hoping that the filenames don't contain special characters for HTML, thus "&" and "<". Granted, for filenames it's not very likely ("<" is even impossible on Windows), but it's a kind of laziness I personally don't appreciate.
        Fair comment.

        If I was doing his myself, I'd almost certainly be using File::Find and CGI, and I'd probably do something like:

        print ("$_",br,"\n") for @files;

        I suppose I could have suggested this to the OP...

        On Unix and Unix-like OSs any character except the path delimiter can be used in filenames, including characters that have special meaning in HTML (like [<>&]) and non-printing characters including CARRIAGE RETURN and NEWLINE. It's a good idea to HTML escape filesystem object names and, depending on the intended use, include some indication of the presence of non-printing characters where they occur.

Re: directory printing
by MaxKlokan (Monk) on Dec 10, 2006 at 17:07 UTC
    As an alternative to using the <br> tag, you could wrap your output with <pre>...</pre> In that case you might want to adjust the style to match the rest of the HTML, if style matters. That should also prevent funny problems with special characters in the filename.