Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Dynamic Directory Index help

by Anonymous Monk
on Oct 07, 2004 at 02:22 UTC ( [id://397203]=perlquestion: print w/replies, xml ) Need Help??

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

I got this code by Matthew Keller from the internet that is supposed to create a dynamic list of links to directories in an HTML file. I run the code through a parser and get errors all over it. Here is the code directly from his site, I of course change the paths to my enviroment:
#!/usr/bin/perl -w sub Get_Title { my =shift; unless(-f "") { return("NO INDEX"); } open(HTML,"<"); while(<HTML>){ if(sh=~ /<title>(.*)<\/title>/i) { close HTML; return ""; } } close HTML; return "Untitled"; } my ="/usr/local/apache/htdocs/projects/"; my ="http://mattwork.potsdam.edu/projects/"; opendir(PRJD,""); my @dirs=readdir PRJD; closedir(PRJD); print "Content-Type: text/html\n\n"; print "<html><head><title>Project Index Page</title></head><body>\n"; for(@dirs) { if(sh=~ /^\./) { next; } unless(-d sh) { next; } my ="sh/index.html"; my =Get_Title(""); =~ s///i; print "<a href=\ "\ "></a>\n"; }
I do not know perl and have been trying to work on this for almost a week now. If anyone could help I would appreciate it. Thanks in advance.

Replies are listed 'Best First'.
Re: Dynamic Directory Index help
by davido (Cardinal) on Oct 07, 2004 at 02:43 UTC

    Did you cut-and-paste exact code, or did you retype the whole thing? Many of the lines of code you posted would never even compile.

    • my = shift; - Huh? That's an error.
    • unless ( -f "" )... - Testing if the empty string is a file?
    • open(HTML, "<"); - Trying to open for input no file at all, and not checking to see if this phantom open even succeeded.
    • if( sh=~/.... - what is 'sh'? Shouldn't you be testing against $_?
    • return "" - return empty string if the match succeeded? Hmm...
    • my ="/usr/local/.... - Shouldn't you be declaring a variable in there somewhere?
    • my ="http://mattwork. - Again, where's the variable name?
    • opendir(PRJD, ""); - Are you opening the empty-string directory?
    • if(sh=~ /^\./)...</code> - What is 'sh'?
    • unless( -d sh)... - What is 'sh'?
    • my ="sh/index.html"; - Why are you still using 'my' without declaring a variable?
    • my =Get_Title("") - Again, no variable.
    • =~ s///i; - Bind s///i to nothing. Even if it were bound to something, it's substituting nothing for nothing. Huh?

    If that's the code you downloaded, it's beyond repair.

    My recommendation is to buy a copy of "Learning Perl", published by O'Reilly & Associates and read it thoroughly. Also read perlintro. And don't bother with this script, just start over once you've read those. If it were just one buggy line or two that would be different, but most of the lines of code you presented contain problems.


    Dave

      I got this code from this website: here

        The engine they used to post the code on the website has destroyed the code. All the variable names are gone. You could put it back together from the article if you were patient.

        If you simply make the directory world readable then apache will give you a much nicer listing.

        chmod 755 /home/you/public_html/somedir/

        cheers

        tachyon

        You are right, that website's code is messed up. I contacted the author , and while I normally wouldn't publically post a private email, the response I received was courteous, prompt, and positive, so with names removed I think it will be ok:

        Hi David,
        I'm answering this query on behalf of (the author) who is unavailable at the moment.

        >><snip>
        >> Someone just posted a question on www.perlmonks.org asking why it
        >> wouldn't work. ...how COULD it work, where all variable names
        >> have been stripped?

        Thanks for your feedback - you're right, there appears to have been some kind of formatting error here.

        We're trying to fix it right now, and I'll let you know once it's done.

        I wonder what wrecked it. Perhaps it was a spell checker or something like that. Anyway, it appears that bart has taken the time to fix it for you, and in fact, it looks like he has improved upon the original script, so I would use his example below. ++bart. :)


        Dave

Re: Dynamic Directory Index help
by bart (Canon) on Oct 07, 2004 at 05:39 UTC
    davido is right, it's mostly the variables that have gone missing, and for some reason $_ seems to have been replaced with "sh". Very bad points for a "technical" site. Anyway, I've tried to fix that, and removed a few bugs in the process (like forgetting about $dir in testing if it's a directory). Here it is:
    #!/usr/bin/perl -w sub Get_Title { my $file = shift; unless(-f $file) { return("NO INDEX"); } local *HTML; local *_; open(HTML,"<$file"); while(<HTML>){ if(/<title>(.*)<\/title>/i) { return $1; } } return "Untitled"; } my $dir ="/usr/local/apache/htdocs/projects"; my $url ="http://mattwork.potsdam.edu/projects"; opendir(PRJD, $dir); my @dirs = readdir PRJD; closedir(PRJD); print "Content-Type: text/html\n\n"; print "<html><head><title>Project Index Page</title></head><body>\n"; print "<h1>Project Index Page</h1>\n"; print "<ul>\n"; for(@dirs) { if(/^\./) { next; } unless(-d "$dir/$_") { next; } my $file = "$dir/$_/index.html"; my $title = Get_Title($file); print "<li><a href=\"$url/$_/index.html\">$_</a>: $title\n"; } print "</ul></body></html>";

    If you don't like the formatting, please don't change the tags. Instead, use CSS to choose different styles for the H1 and LI tags. You'll probably want a smaller H1 font, and you can remove or replace the bullets from the list, for example.

Re: Dynamic Directory Index help
by krujos (Curate) on Oct 07, 2004 at 02:35 UTC
    you are missing a bunch of variable names. Every time you see my there should be a variable name following. For example:
    my ="/usr/local/apache/htdocs/projects/"; my ="http://mattwork.potsdam.edu/projects/";
    should look like this line does
    my @dirs=readdir PRJD;
    add the following to the top of your script and you will get more helpful error messages
    #!/usr/bin/perl use warnings use strict
    check out this book (Learning Perl) by our own merlyn for a good place to start with perl.
      Thanks for your reply. I have my output here:
      [Wed Oct 06 09:53:06 2004] [error] [client 192.168.2.254] Unquoted str +ing "sh" may clash with future reserved word at /www/cameo/htdocs/ind +ex.pl line 8. [Wed Oct 06 09:53:06 2004] [error] [client 192.168.2.254] Unquoted str +ing "sh" may clash with future reserved word at /www/cameo/htdocs/ind +ex.pl line 25. [Wed Oct 06 09:53:06 2004] [error] [client 192.168.2.254] Unquoted str +ing "sh" may clash with future reserved word at /www/cameo/htdocs/ind +ex.pl line 26. [Wed Oct 06 09:53:06 2004] [error] [client 192.168.2.254] Backslash fo +und where operator expected at /www/cameo/htdocs/index.pl line 30, ne +ar ""<a href=\\$ [Wed Oct 06 09:53:06 2004] [error] [client 192.168.2.254] \t(Missing o +perator before \\?) [Wed Oct 06 09:53:06 2004] [error] [client 192.168.2.254] syntax error + at /www/cameo/htdocs/index.pl line 4, near "my =" [Wed Oct 06 09:53:06 2004] [error] [client 192.168.2.254] syntax error + at /www/cameo/htdocs/index.pl line 17, near "my =" [Wed Oct 06 09:53:06 2004] [error] [client 192.168.2.254] syntax error + at /www/cameo/htdocs/index.pl line 27, near "my =" [Wed Oct 06 09:53:06 2004] [error] [client 192.168.2.254] syntax error + at /www/cameo/htdocs/index.pl line 30, near ""<a href=\\ "\\" [Wed Oct 06 09:53:06 2004] [error] [client 192.168.2.254] Execution of + /www/cameo/htdocs/index.pl aborted due to compilation errors. [Wed Oct 06 09:53:06 2004] [error] [client 192.168.2.254] Premature en +d of script headers: index.pl
      Do you know which varibles I would need to have? I am not a coder at all. I try but just never grasped it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://397203]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-16 22:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found