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

I have this little script that reads from 'Chat'-directory and print output to web-page. What I need is addenum which makes those lines clickable links pointing that file in the directory....help is much appreciate here is the code
#!/usr/bin/perl use CGI::Carp qw(fatalsToBrowser); $dirtoget="chat"; opendir(IMD, $dirtoget) || die("Cannot open directory"); @thefiles= readdir(IMD); closedir(IMD); print "Content-type: text/html\n\n"; print "<html><body>"; foreach $f (@thefiles) { unless ( ($f eq ".") || ($f eq "..") ) { print "$f<br />"; } } print "</body></html>";

Replies are listed 'Best First'.
Re: Links to web page?
by almut (Canon) on Dec 03, 2008 at 16:11 UTC

    What you want sounds like the functionality of the Apache module mod_autoindex.  So, in case you're using that web server, it's just a matter of configuration...

    Update: That said, if your primary intention isn't just to get it working, but rather to learn some CGI/HTML programming, you could of course also write such a module yourself in Perl... Just say so, and I'm sure the monks will be happy to help :)

      I dont understand that link at all..sorry... Kari

        Are you using Apache?  If so, mod_autoindex can give you automatically generated file listings (HTML pages) of directories the server is configured to access — in case no other explicit index (index.html) is found for the respective directory. Files will typically appear as clickable links, but you can flexibly configure the appearance in many ways (file sorting, with icons, descriptions, etc.).

        To get you started, here's a sample configuration snippet (incomplete) for your httpd.conf:

        LoadModule autoindex_module modules/mod_autoindex.so LoadModule alias_module modules/mod_alias.so Alias /chat /path/to/directory/chat # ...so you can access the chat directory via http://servername/chat <Directory /path/to/directory/chat> Options +Indexes IndexOptions FancyIndexing # ... </Directory>
Re: Links to web page?
by moritz (Cardinal) on Dec 03, 2008 at 17:08 UTC
    Just configure your web server to return a directory index page when accessing the directory.

    If you really want to do it your way, look up how a link is made in HTML - it's not all that complicated.

      I know how to make link with HTML but I do not know how to adapt it to this part of my code??
      { print "$f<br />"; }

        You need to wrap the name of the file inside an anchor tag. I think you should follow the advice given so far, but I can't help but try to impress you with this :)

        use strict; use warnings; use CGI qw(:all); print header(), ul( map li( a( {href => $_}, $_ ) ), <*.*> ) ;

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)