Solved

Connecting CGI Perl script to other files

Posted on 2003-11-25
7
545 Views
Last Modified: 2013-12-25
Hi, I'm trying to implement a very basic guestbook on my webpage based on an example in a textbook. The HTML form works fine, but there is a problem with the Perl/CGI (extra print statements added to aid debugging):

#!/usr/bin/perl -wT
use strict;
use CGI qw(:all);
use Fcntl qw(:flock);

# Location of the guestbook log file.
my $gbdata="http://www.mysite.net/data/log.txt";
my $semaphore_file="http://www.mysite.net/temp/sample.sem";

# Function to lock (waits indefinitely)
sub get_lock {
   open(SEM, ">$semaphore_file")
       || die "Cannot create semaphore: $!";
      print "<br /> done open";
   flock SEM, LOCK_EX;
      print "<br /> done lock";
}

# Function to unlock
sub release_lock {
    close(SEM);
}

# This function saves a passed-in help desk HTML form to a file
sub save {
     print "<br />Saving";
     get_lock();
      print "1";

     open(GB, ">>$gbdata") || die "Cannot open $gbdata: $!";
      print "2";
     print GB "name: ", param('name'), "\n";
     print GB "e-mail: ", param('email'), "\n";
     print GB "wpage: ", param('wpage'), "\n";
     print GB "entry: ", param('problem'), "\n";
     close(GB);
     release_lock();
}

# This function displays the contents of the help desk log file as HTML,
# with minimal formatting.
sub display {
     print "<br />Displaying";
      print "<br />The name was ", param('name');
     open(GB, $gbdata) || die "Cannot open $gbdata: $!";
     while(<GB>){
        print "Another entry:";
          print "<b>$_</b><br />";  # The name
          my($email,$wpage,$entry);
          $email=<GB>;
          $wpage=<GB>;
        $entry=<GB>;
          print "$email<br />";
          print "$wpage<br />";
        print "$entry<br /><br />";
     }
     close(GB);
}

print header;
print "Guest book under development";
# The parameter 'submit' is only passed if this CGI program was
# executed by pressing the 'submit' button in the form in listing 18.7

if (defined param('submit')) {
     save;
      print "<br />Finished saving";
     display;
} else {
     print "<br />Doing display only";
     display;
}
# END OF CODE

The problem is that the program gets stuck at the following line in sub get_lock:    open(SEM, ">$semaphore_file")
Or, if I forget about locking by removing get_lock and release_lock, the program gets stuck at the following line in sub save:   open(GB, ">>$gbdata") || die "Cannot open $gbdata: $!";
In neither case is the message in the 'die' statement printed on the web-page.

So the problem is that the program fails to create the semaphore file or open the log file (for the guestbook messages). I have set permissions as instructed by my web-hosting company.

Anybody any idea what might be causing the problem? Many thanks in advance for your help.
0
Comment
Question by:million1977
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 84

Assisted Solution

by:ozo
ozo earned 50 total points
ID: 9822366
you probably don't want the "http://www.mysite.net" in your $gbdata or
$semaphore_file
0
 

Author Comment

by:million1977
ID: 9825329
thanks, but that hasn't made any difference
0
 
LVL 5

Accepted Solution

by:
apparition earned 50 total points
ID: 9826723
is the script being run in a cgi-bin directory?
if so then use
my $gbdata="data/log.txt";
my $semaphore_file="temp/sample.sem";

and create blank files "data/log.txt" and temp/sample.sem
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 3

Assisted Solution

by:rkosai
rkosai earned 50 total points
ID: 9827014
You will also want to change the permissions on the files/directories mentioned by apparition, make sure they're set to something like 766, so that your script can write to those locations.

Having the "http://" in your file path will not give the intended result; its better to code in a relative directory (either "../data/file.txt" if its outside your cgi-bin directory, or "data/file.txt").
0
 
LVL 51

Assisted Solution

by:ahoffmann
ahoffmann earned 50 total points
ID: 9830205
use CGI::Carp   qw(fatalsToBrowser);
0
 

Author Comment

by:million1977
ID: 9859269
Sorted the problem, I'm new to Perl & CGI so all your comments were useful.
Found ahoffmann's particularly helpful for debugging.
50 points to each of you
0
 

Author Comment

by:million1977
ID: 9885008
Sorry about the hold-up people, the link to Split Points isn't working for me, have messaged the Community Support board so hopefully it'll be sorted soon.

apologies again
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction:   Welcome to my first article ever. To begin with, the reason I write this article.  I participated in a question on Experts Exchange about the start command in Windows and there were some discussion about the usage. The discussio…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The viewer will learn how to count occurrences of each item in an array.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question