[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Connecting CGI Perl script to other files

Posted on 2003-11-25
7
Medium Priority
?
551 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 200 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 200 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
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
LVL 3

Assisted Solution

by:rkosai
rkosai earned 200 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 200 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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

Introduction This tutorial will give you a fast look what you can do with WhizBase. I expect you already know how to work with HTML at least, and that you understand the basics of the internet and how the internet works. WhizBase is a server-s…
If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Suggested Courses

656 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