Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

a quick stab at a public links page

by Anonymous Monk
on Jul 20, 2001 at 07:51 UTC ( #98316=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

i wrote a rather shabby public links page a while ago which stayed on my win2k box for a while working perfectly, then when decided to upload it to a server it stops on the following line:

my (%params) = $query->Vars;

with no errors or anything. it just stops any ideas why, oh monks of great wisdom?

here's the full code
#!/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

    How do you know it stops at line 12? Is there an error? What is happening that makes you certain it is this line?

    Try adding use CGI::Carp 'fatalsToBrowser'; and run the program through a browser. Is there a error now? Let us know.

    BTW, what operating system is the server running? I assume it's not win2k.

    Charles K. Clarkson
      i was putting print "m00"; before and after each line until i worked out where it was stopping :) then i tried taking the line out. (with other tweaks so i could)
      ill just trundle off to find out what its running, ive seen it somewhere around the site...
      ok, here's what it says:
      Portland web servers are based around IBM AIX and IBM RS/6000 web servers.

      ok, ive added the fatalsToBrowser line (assumed it was in already, which is why i was getting *really* confused!) and get the following:

      Software error:

      Undefined subroutine CGI::Vars

      For help, please send mail to the webmaster (, giving this error message and the time and date of the error.

      im still not really much enlightened, surely use CGI; should be sorting that?

      hope you can help!

           While not certain, I suspect the server may have an older version of installed. Perhaps a fellow monk knows when Vars was added to If you can get a look at the source on your server, search for the Vars subroutine. You should be able to determine the version of CGI by printing $CGI::VERSION. Install the newest version from CPAN if you can. Try a Super Search on Vars

        If all else fails you could bother the author, but wait a few days for a reply here first.

        Charles K. Clarkson
Re: a quick stab at a public links page
by CharlesClarkson (Curate) on Jul 22, 2001 at 20:35 UTC

    I took a closer look at your script and you don't need CGI::Vars at all. You may want to read the documentation on Your missing a lot of oppurtunities. Here are few corrections that may have you up and going quickly.

    On this line we're creating %params.
    my (%params) = $query->Vars; has a function for this: param. To access any parameter, just call param('any'). To use it as a function we need to change use CGI; to use CGI qw/param/;

    Now that we can access param directly, let's make a few changes.

    my ($command) = $params{'command'}; touch() if ($command eq "touch"); addlink() if ($command eq "add");
    touch() if param('command') eq 'touch'; addlink() if param('command') eq 'add';
    errorpage("You must fill in both fields!") if (($params{'name'} eq "") || ($params{'url'} eq ""));
    errorpage('You must fill in both fields!') if param('name') eq '' || param('url') eq '';
    print FILE $params{'name'}." ".$params{'url'}."\n";
    becomes: print FILE param('name'), ' ', param('url'), "\n";

    Charles K. Clarkson

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://98316]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2018-06-25 12:45 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.