Building PERL Scripts on a Windows system*

whooploafSoftware and s/w Development

Dec 13, 2013 (3 years and 7 months ago)

78 views

Building PERL Scripts on a
Windows system*


*and running those scripts on an
Apache server!

Tools Used


Windows XP service pack 3 computer


GoDaddy account for hosting


Domain name


Filezilla for FTP access


DOS to Unix conversion tool

The Dilemma


I wanted to further master my web programming
skills after completing HTML, Javascripting and PHP
courses at Chaffey


I signed up for Perl/CGI


Hosted my website www.nickburns.com with
GoDaddy after discussing my needs with them


I then found for CGI, they use an Apache server!


This created the need to learn how to convert DOS
(Windows) derived files into Unix/Linux files so the
server would be able to run them correctly


The Solution


I found, after quite a bit of searching and
testing, a simple and powerful utility which
will translate “fromDOS” or “toDOS” based on
the user’s needs


Now, the process…

Build a Set of Files


Build the html page first


Point the html to the cgi file


Upload the HTML page to the web server


Build the CGI file(s)


Convert the cgi file(s) into Unix type file(s)


Upload the converted cgi to the web server
using Filezilla


Change the cgi file attributes to 755


Disconnect from the server


Test by connecting to the html web page


HTML Page


<!book.html>


<HTML>


<HEAD><TITLE>Jubilee Book Club</TITLE></HEAD>


<BODY>


<H1 ALIGN=center>Jubilee Book Club Home
Page</H1><HR>


<H2 ALIGN=center>Click the book to sign in<BR>


<A HREF="/cgi/book1.cgi">
Points to the CGI file


<IMG SRC=book.jpg></A></H2>


</BODY></HTML>


CGI Script
(yes, it is a bit small; bear with me)

#!/usr/bin/perl

#book1.cgi
-

displays a sign
-
in form

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

use CGI qw(:standard);

use strict;


#declare variable

my $name;


#retrieve Name cookie

$name = cookie('Name');


print "<HTML>
\
n";

print "<HEAD><TITLE>Jubilee Book Club</TITLE></HEAD>
\
n";

print "<BODY>
\
n";

print "<H1>Jubilee Book Club Sign
-
In Form</H1><HR>
\
n";

print "<FORM
\
n";

print "ACTION='book2.cgi'
\
n";

print "METHOD=POST>
\
n";


print "<TABLE>
\
n";

print "<TR><TD>Name:</TD><TD>
\
n";

print "<INPUT TYPE=text NAME=Name SIZE=25 VALUE='$name'>
\
n";

print "</TD></TR>
\
n";

print "</TABLE>
\
n";


print "<BR><INPUT TYPE=submit VALUE=Submit>
\
n";

print "</FORM></BODY></HTML>
\
n";



CGI Script Described


Shebang line


UNIX


#!/usr/bin/perl
(be very careful to get this right; I once
spent hours debugging a script only to discover I had typo’d on
this line)


File name and description of what the file will
perform
-

#book1.cgi
-

displays a sign
-
in form


Declare the type of page
-

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

-

HTML for example


Use the # for comments

CGI Script Described (continued)


Use declarations
-

use CGI qw(:standard);

-

to tell Perl what module to use


Prevent Perl from creating undeclared
-

use strict;


Perl statements end with a semi
-
colon


Declare variable
-

my $name;


Add a cookie
-

$name = cookie('Name');

CGI Script Described (continued)


Print/Return an HTML page


Code in much the same way as a standard
HTML page with print statements:


print "<HTML>
\
n";


print "<HEAD><TITLE>Jubilee Book Club</TITLE></HEAD>
\
n";


print "<BODY>
\
n";


print "<H1>Jubilee Book Club Sign
-
In Form</H1><HR>
\
n";


print "<FORM
\
n";


print "ACTION='book2.cgi'
\
n";


print "METHOD=POST>
\
n";

CGI Script Described (continued)


print "<TABLE>
\
n";


print "<TR><TD>Name:</TD><TD>
\
n";


print "<INPUT TYPE=text NAME=Name SIZE=25
VALUE='$name'>
\
n";


print "</TD></TR>
\
n";


print "</TABLE>
\
n";



print "<BR><INPUT TYPE=submit VALUE=Submit>
\
n";


print "</FORM></BODY></HTML>
\
n";


Remember to close each object!


This cgi also points to a 2
nd

cgi file containing
the cookie, which will greet the customer

CGI Script Described (continued)


Open the 2
nd

CGI file in same manner as the
first with a different file name:


#!/usr/bin/perl


#book2.cgi
-

displays a Web page containing the user's


#name and the book information


use CGI qw(:standard);


use strict;



#declare variables


my ($name, $C_name);


#assign input to variable


$name = param('Name');

CGI Script Described (continued)


Create the cookie:


$C_name = cookie(
-
name => "Name",

(input from the form)




-
value => "$name",
(variable to save)




-
path => "/cgi",
(path where to save the cookie)




-
expires => "+6M");
(how long to save the cookie)



Send the cookie to the browser:


print header(
-
cookie => $C_name);




CGI Script Described (continued)


Create the response web page:


print "<HTML>
\
n";


print "<HEAD><TITLE>Jubilee Book Club</TITLE></HEAD>
\
n";


print "<BODY>
\
n";


print "<H1 ALIGN=center>Hello, $name!<BR>
\
n";


print "The book of the month is</H1><HR>
\
n";


print "<H2 ALIGN=center><FONT COLOR=red>
\
n";


print "<I>The Case of the Missing Dagger</I>
\
n";


print "<BR>by H.T. Sims
\
n";


print "</FONT></H2>
\
n";


print "</BODY></HTML>
\
n";


Script is now complete



Format the CGI File(s)


After following the simple directions in their
readme file and installing “fromDOS”


Open a command prompt


Change directory to the location of the cgi file(s)


Type
fromDos filename.cgi
and press
Enter


The file is now compatible with Unix


Type
perl

c filename.cgi
and press
Enter


Perl starts the perl interpreter


-
c checks the syntax without executing


If no errors, proceed to upload


Uploading the CGI files


Open Filezilla


Set up a connection to the server with appropriate
user name and password


Left side window is location of local files


Right side window is location of server side files


Usually cgi
-
bin is the desired directory for cgi files,
but GoDaddy uses CGI as their directory


Upload the converted files to the server’s CGI folder


Right click on the server files and select File Attributes


Change attributes to 755 and disconnect when complete

Testing


Open a Browser


Connect to the path of the HTML file uploaded


Complete the form as necessary


Determine if the results are as expected


Finished