Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/local/bin/perl -w print "Content-type: text/html\n\n"; my $filename = "/home/user/cgi-bin/database.txt"; use CGI; #use strict; my ($query) = new CGI; my $cgi_path = $query->script_name; $cgi_path =~ s/(^.*\\)//; my (%params) = $query->Vars; my ($command) = $params{'command'}; touch() if ($command eq "touch"); addlink() if ($command eq "add"); unless (open FILE, $filename) { nofile(); } my @slurp = <FILE>; close (FILE); print<<END; <html> <head> <title>Links</title> </head> <body> <H1>Links Page</H1><hr><p> <form method=POST action="$cgi_path"> Sorry, adding links is not working at the moment. Can anyone tell me w +hy \%params = \$query->Vars; causes the script to stop?<br> <table> <tr><h3>Add Link</h3></tr><br> <input type="hidden" name="command" value="add"> <tr><td>Name</td><td><input type="text" name="name" size="40"></td></t +r> <tr><td>Url</td><td><input type="text" name="url" size="40"></td></tr> <tr><td></td><td><input type="submit" value="Submit"></td></tr> </table> </form> <hr> END my $flag=0; my ($label, $i, @group, $line, $labeltmp); foreach $line (@slurp) { if ($line =~ m/(\#label)(\s+)(.*)/g) { $labeltmp=$3; if ($flag == 1) { print "<h2>".$label."</h2><p>\n<ul>\n"; @group = sort (@group); foreach (@group) { if (m/(.+)(\s+)(\S+$)/g) { print "<li><a href=\"$3\">$1</a><br>\n"; } } print "</ul><hr>"; undef(@group); } $i=0; $label=$labeltmp; $flag=1; } else { $i++; $group[$i]=$line; } } print "<h2>".$label."</h2><p>\n<ul>\n"; @group = sort (@group); foreach (@group) { if (m/(.+)(\s+)(\S+$)/g){ print "<li><a href=\"$3\">$1</a><br>\n"; } } print "</ul><hr>"; print<<END; </body> </html> END exit; sub nofile { print<<END; <html><head><title>Fatal Error!</title></head><body> Can't read \'$filename\'<br> <a href="$cgi_path?command=touch">Click here to create it</a> </body></html> END exit(0); } sub touch { unless (open FILE, ">$filename") { errorpage("Help! Can't touch file") +; } print FILE "#label Computers\n"; print FILE "site url\n"; close (FILE); } sub addlink { unless (open FILE, ">>$filename") { errorpage("Help! Can't open databa +se for appending"); } errorpage("You must fill in both fields!") if (($params{'name'} eq "") + || ($params{'url'} eq "")); print FILE $params{'name'}." ".$params{'url'}."\n"; close (FILE); print<<END; <a href="$cgi_path">Return</a> END exit; } sub errorpage { print "<html><head><title>Fatal Error!</title></head><body>\n"; print @_; print "</body></html>"; exit(0); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: a quick stab at a public links page
by CharlesClarkson (Curate) on Jul 20, 2001 at 08:36 UTC | |
by Anonymous Monk on Jul 20, 2001 at 21:33 UTC | |
by CharlesClarkson (Curate) on Jul 21, 2001 at 03:35 UTC | |
by pmas (Hermit) on Jul 21, 2001 at 08:57 UTC | |
|
Re: a quick stab at a public links page
by CharlesClarkson (Curate) on Jul 22, 2001 at 20:35 UTC |