Forum Moderators: coopster & phranque

Message Too Old, No Replies

Perl Database On To Website

Integrating Perl & HTML

         

Mully1941

12:07 pm on Mar 13, 2007 (gmt 0)

10+ Year Member



Hi All,

Newbie with probably a daft question, using a small Perl database that I downloaded and altered to do what I wanted and it works OK on the Command Prompt on my machine. The question is how do I get it onto my website. Below is the Perl Code and I have the text file saved in Notebook.

#!/usr/bin/perl -w

# Distances.cgi: a very simple database application.
# This program reads a text file that is a distance
# database and lets you query it by RPRA number.
#
# Each record is on a single line. Each field in the
# record is separated by a single tab character
# ("\t"). The database has six fields:

# - RPRA number
# - Last name
# - First name
# - Miles
# - Yards
# - Racepoint

# The name of the database file
$TheDB = 'Distances.txt';

# Open the database file but quit if it doesn't exist
open(INDB, $TheDB) or die "The database $TheDB could " .
"not be found.\n";

while(1) { # Loop forever
print "\nEnter Members RPRA Number To Find Their Distances (Y), " .
" or quit (Q): ";
$DoSearch = <STDIN>;
chomp($DoSearch);
$DoSearch =~ tr/A-Z/a-z/;
# Check if they want to quit
if($DoSearch eq 'q') { last }
# Check if they did *not* say y or Y
unless($DoSearch eq 'y') {
print "You must enter either Y or Q.\n";
next; # Go out to the while loop
}

# Ask them what RPRA Number they want to search for
print "Enter Members RPRA Number: ";
$SearchFor = <STDIN>;
chomp($SearchFor);
# Go to the top of the database in case this isn't
# the first time they are searching
seek(INDB, 0, 0);
# Reset the count of successes
$SuccessCount = 0;
# Loop through the records in the file
while(<INDB>) {
$TheRec = $_;
chomp($TheRec);
($RPRA, $LastName, $FirstName, $Racepoint, $Miles,$Yards) =
split(/\t/, $TheRec);
if($RPRA eq $SearchFor) {
$SuccessCount = $SuccessCount + 1;
print "$RPRA: $FirstName $LastName, $Racepoint, $Miles, ".
"$Yards\n";
} # End of if
} # End of while(<INDB>)
if($SuccessCount == 0) { print "No records found.\n" }
else { print "$SuccessCount records found.\n" }
} # End of while(1)

print "Program finished.\n";

Any help or guidance in the right direction would be much appreciated.

Matt Probert

1:47 pm on Mar 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to research "CGI". This is the main method by which http servers interact with external programs, such as Perl, usually through a <form> element in an html page.

Matt

rocknbil

8:19 pm on Mar 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard Mully, as presented this will not run on your server. Matt's correct, dig up a few perl tutorials on what you need to to do get perl to interact with your website.

A perl script requested by a website requires that you tell the CGI gateway what type of content the program is sending - you would need to add

print "content-type: text/html\n\n";

at the top of the script.

Then there are a few other issues you'll need to address:

- Notepad is in PC format, when you upload it to a Linux server, it may not run because you need to insure the end-of-line characters correctly convert to Unix end-of-line characters. Some server will manage this, some won't. The only way to be sure is to use an editor that saves files in Unix format.

- The file must have permissions to execute to run, look up the command "chmod." (file mode)

- The database *may* be found from the same directory as the script, but on some systems it may not. For example, when you run perl, on some systems the "working directory" changes to wherever perl actually is, so when you say "open(INDB, $TheDB)" it may be trying to look in the /usr/bin/perl directory. You may need to specify the /full/system/path/to/Distances.txt

I know you may not get all of what I said but search around for perl tutorials and much of this will begin to make sense.

phranque

7:23 am on Mar 14, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



greetings Mully1941 and welcome to WebmasterWorld!

you cannot run a perl program within a web browser as if it were the command line.
you must essentially collect all the user input in a form in the web browser and submit the collected parameter key/value pairs to a (perl cgi) script on the server which uses the provided information to do some work and return a document to the browser.

Notepad is in PC format, when you upload it to a Linux server, it may not run because you need to insure the end-of-line characters correctly convert to Unix end-of-line characters. Some server will manage this, some won't. The only way to be sure is to use an editor that saves files in Unix format.

rocknbil:
not really an issue.
i use ms-based wysiwyg tools to edit unix-based source all the time.
you can usually set up your ftp client to handle this on a per-filetype basis.
or you can create a perl script to filter those characters when necessary.

in an emergency you can open an afflicted file in the vi editor and type the following command to remove the last character of each line in the file:

:1,$ s/.$//

Mully1941

9:42 am on Mar 14, 2007 (gmt 0)

10+ Year Member



Hi All

You are certainly sending me in new directions, down to the local library later to see what I can find -- probably nothing of much use there, the Internet will and should help guide me towards solving my problem -- think the reference to Notepad is also very helpful.You see as an OAP ( Senior Citizen ) if we stop trying to learn new skills or educate ourselves the old grey matter stops working and sends us nearer to that final sunset.

Just downloaded Text Pad will have a go with that later after my library visit.

Many Thanks

To You all for your Help.

Mully

perl_diver

7:05 pm on Mar 14, 2007 (gmt 0)

10+ Year Member




in an emergency you can open an afflicted file in the vi editor and type the following command to remove the last character of each line in the file:

or use an FTP application which should silently take care of the line ending problem during the file transfer.

phranque

7:38 pm on Mar 14, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



or use an FTP application which should silently take care of the line ending problem during the file transfer.

p_d:
that's why i wrote this:

you can usually set up your ftp client to handle this on a per-filetype basis.

=8)

mully:
if you are just getting into this space, check out "CGI Programming with Perl" published by O'Reilly [oreilly.com].
there are lot's of online references but if you prefer dead trees you will probably tend toward the o'reilly reference series...

perl_diver

3:21 am on Mar 15, 2007 (gmt 0)

10+ Year Member



phranque,

sorry mate, I missed that part of your post.

rocknbil

9:33 am on Mar 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



not really an issue.

You are assuming a beginner is going to know how to set up an FTP client to manage end of line chacters, or even know what vi, pico, or nano is. It is one of the most common fixes I apply when people call me, right up there with file mode and ASCII upload.

phranque

10:29 am on Mar 15, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



You are assuming a beginner is going to know how to set up an FTP client to manage end of line chacters, or even know what vi, pico, or nano is. It is one of the most common fixes I apply when people call me, right up there with file mode and ASCII upload.

what you say is true.
but if you can't figure out if and/or how to handle end of line characters, the whole perl/cgi thing is irrelevant.

i say throw him in and see if he swims...