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

Hello, I am now learning Perl for two months. Until now, I am doing well. Most of the time, I can solve problems myself just by reading textbook. Now I have a trouble for the first time. I typed exactly what the textbook example shows, but I can not create/open a DBM database. I would like to know why.... I tested from command line, but there were no errors, but no response. Just the prompt appeared.

This is a program that accept the visitor's name and e-mail from an html page, and then store them in a DBM database. Or, if the user wants to remove his/her info from the mailing list, the program remove them. Here is the code:
#!/usr/bin/perl #band.cgi - saves data to and removes data from a database #creates appropriate dynamic Web pages print "Content-type: text/html\n\n"; use CGI qw(:standard -debug); use SDBM_File; use Fcntl; #prevent Perl from creating undeclared variables use strict; #declare variables my ($button, $name, $email); #assign values to variables $button = param('Button'); $name = param('Name'); $email = param('Email'); if ($button eq "Put Me On Your Mailing list") { add(); } elsif ($button eq "Remove Me From Your Mailing List") { remove(); } exit; #*****user-defined functions***** sub add { #declare variable my %mail; #open database, add record, close database tie(%mail, "SDBM_File", "maillist", O_CREAT|O_RDWR, 0666) or die "Error opening maillist. $!, stopped"; $mail{$email} = $name; untie(%mail); #create Web page print "<HTML>\n"; print "<HEAD><TITLE>The Jeffrey Sikes Band</TITLE></HEAD>\n"; print "<BODY BGCOLOR=silver>\n"; print "<FONT SIZE=5>\n"; print "<H1>The Jeffrey Sikes Band</H1>\n"; print "Thank you, $name. We will send the monthly \n"; print "newsletter to $email.\n"; print "</FONT></BODY></HTML>\n"; } #end add sub remove { #declare variables my (%mail, $msg); #open database tie(%mail, "SDBM_File", "maillist", O_RDWR, 0) or die "Error opening maillist. $!, stopped"; #determine if user's information is in the database if (exists($mail{$email})) { delete($mail{$email}); $msg = "Thank you, $name. We have removed your "; $msg = $msg . "information from our mailing list."; } else { $msg = "You are not on our mailing list."; } #close database untie(%mail); #create Web page print "<HTML>\n"; print "<HEAD><TITLE>The Jeffrey Sikes Band</TITLE></HEAD>\n"; print "<BODY BGCOLOR=silver>\n"; print "<FONT SIZE=5>\n"; print "<H1>The Jeffrey Sikes Band</H1>\n"; print "$msg\n"; print "</FONT></BODY></HTML>\n"; } #end remove

Replies are listed 'Best First'.
Re: I can't connect to DBM Databases
by naikonta (Curate) on Oct 16, 2007 at 01:44 UTC
    Could you please show us the message from the error log? You put $! in your die statement so the error log may give some clue. Beware that you might have a permission problem.

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

      Hello, naikonta: Here is the copy of the log when I tested the band.cgi from the command line. There were no error messages. Or, should I see other error log somewhere. I leaned to use die function. But, I didn't know wherelse to see error messages.

      If it's the permission problem, could I do anything on the code? Shebang line? Or else? Thank you.
      [yyokot01@crux chap09]$ perl -c band.cgi band.cgi syntax OK [yyokot01@crux chap09]$ perl -w band.cgi Content-type: text/html (offline mode: enter name=value pairs on standard input; press ^D or ^ +Z when done) Button=Put+Me+On+your+Mailing+List Name=Jane Emai=jc@pop.com [yyokot01@crux chap09]$

        Maybe this helps:

        use CGI::Carp qw(fatalsToBrowser);
        fromjp,

        Basically, running a CGI program is quite differently from running command line program. I'm afraid you need to obtain some basic knowledge about CGI programs at general. I hope you can find some suitable tutorials for begineer on CGI. You can start with google, or CGI Programming tutorial.


        Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re: I can't connect to DBM Databases
by shmem (Chancellor) on Oct 16, 2007 at 05:37 UTC
    tie(%mail, "SDBM_File", "maillist", O_CREAT|O_RDWR, 0666) or die "Error opening maillist. $!, stopped";
    Do you know where in the file system you are letting the web-server user open/create the file 'maillist'? Make sure that the location is writable, and the path to it is executable for this user.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      >Do you know where in the file system you are letting the web-server user open/create the file 'maillist'? Make sure that the location is writable, and the path to it is executable for this user. --shmem


      This is the input form of the starting html page(I changed the server name and my folder name here). I think all the created files should be stored into cgi-bin/chap09. I tested and tested, but I could not create files into cgi-bin/chap09.
      <!band.html> <HTML> <HEAD><TITLE>Jeffrey Sikes Band</TITLE></HEAD> <BODY BGCOLOR=silver> <H1><IMG SRC="band.gif" ALIGN=middle HSPACE=5>The Jeffrey Sikes Band</ +H1> <FORM ACTION="https://(servername)/(myfolder)/cgi-bin/chap09/band.cgi" + METHOD=POST> <FONT SIZE=4> We play the blues like you have never heard before. Sign up on our mailing list to receive a free monthly newsletter that tells you when and where we will be playing during the month. <P>Name: <INPUT TYPE=text NAME=Name>&nbsp;&nbsp; E-mail address: <INPUT TYPE=text NAME=Email></P> <INPUT TYPE=submit Name=Button VALUE="Put Me On Your Mailing List"> <INPUT TYPE=submit Name=Button VALUE="Remove Me From Your Mailing List +"> </FONT></FORM></BODY></HTML>
        Don't store data underneath cgi-bin. Use an absolute path outside cgi-bin (e.g. /var/www/data) and change the ownership of that directory to the apache user.

        --shmem

        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      >Do you know where in the file system you are letting the web-server user open/create the file 'maillist'? Make sure that the location is writable, and the path to it is executable for this user.

      I know the path and the folder the databases are saved. I tested a similar program (c09ex5.cgi).
      * I gave permission 755 for perl -c c09ex5.cgi.
      * This time, I successfully created two database files, c09ex5.dir and c09ex5.pag.
      * The permissions for them were default 644. I didn't change the permissions for the created DB files.
      * This program still does not run on the Web.
      * I also tried to give 777 permission to the folder chap09. This folder contains c09ex5.cgi, c09ex5.pag, and c09ex5.dir.

      What else I should do... Should I give 755 for cgi-bin folder?