Hello World in Perl

helmetpastoralSoftware and s/w Development

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

45 views

ITU


UNIX Operating System

Project #1


Hello World in Perl


Step 1
Hello.pl

1.1) I
n your home directory, make

a
directory called
public_html
. Then, go in this
directory and make a
file called
hello.pl

with this one line:


print "Hello World
\
n";

Make su
re the PERL executable is on your path.

1.2)
Now type the following command at your command prompt:


perl hello.pl

If everything's OK, you should get the following output:


Hello World

Step 2
Hello.cgi

2.1) make
a
new
file called
hello.cgi

with these line
s of code:

#!/usr
/bin/perl

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

print "<H1>Hello World</H1>
\
n";

Note that the top line is the path to perl on your unix host.


2.2)
Now type the following command at your command prompt:


perl hello.cgi

If everything's OK,

you should get the following output:

Content
-
type: text/html


<H1>Hello World</H1>


2.3)
Next, execute this command:


perl hello.cgi > junk.htm

Now open file
junk.htm

in your browser.

You should get

Hello World in very large letters. If not, troubleshoo
t.

2.4) Make

hello.cgi executable.
chmod

a+x hello.cgi should do it. Now, type this
command:


perl ./hello.cgi

Your output should look like this:

Content
-
type: text/html


<H1>Hell
o World</H1>

If it doesn't, check to see if
you are pathed
correctly for perl. Try this:


perl
-
v

Note the output it produces.

2.5)
Once you get the
perl ./hello.cgi

command to work correctly, try this:


./hello.cgi

Once again, you should get th
e same out
put as in

step 2.4

If you don't, use the command
which perl

to see where perl resides on the machine, and
change the top line of
hello.cgi

accordingly. If that doesn't fix it, troubleshoot.

2.6)
Once you get the right output typing just the filename, creat
e
a new file

hello.htm

in the
public_html

directory:

Content
-
type: text/html


<P><A HREF="hello.cgi">RUN hello.cgi</A>.</P>

Now pull up hello.htm in your browser, click on the hyperlink that says "RUN hello.cgi.
If everything's cool it should come back wit
h a screen saying Hello World in large letters.
Otherwise, troubleshoot.


Step 3
HelloForm.cgi

3.1)
Create HelloForm.cgi in a directory on the server, as follows:

HelloForm.cgi

#!/usr/local/bin/perl


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


print "Hello Wor
ld.
\
n";


print "Heres the form info:<P>
\
n";


my($buffer);


my(@pairs);


my($pair);


read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'});


@pairs = split(/&/, $buffer);


foreach $pair (@pairs)


{


print "$pair<BR>
\
n"


}

print "<P>Note that further parsing is
\
n
";

print "necessary to turn the plus signs
\
n";

print "into spaces and get rid of some
\
n";

print "other web encoding.
\
n";

#path to perl

#so output produces web page

#we believe in tradition


#various declarations



#POST method uses stdin

#Control/value pai
rs &
delimited

#print each ctrl/value pair

3.2)
Now make it executable with
chmod
:

chmod a+x HelloForm.cgi

3.3)
Now
add the
following
lines to your hello.htm file
in the
public_h
tml
directory

(same directory

as HelloForm.cgi
)
:

<form action="FormHello.cgi" method="POST">


<P>Text: <input type="text" size="20" name="TextLine">


<input type="submit" name="Button" value="Submit"></P>

</form>

3.4)
Access the
hello.htm
web page, f
ill in the text line, click the button, and you'll see
the results come up.