Troubleshooters.Com and Code Corner Present

Steve Litt's Perls of Wisdom:
Hello World in Perl
(With Snippets)

Copyright (C) 1998 by Steve Litt


Hello.pl

Save a file called hello.pl with this one line:

print "Hello World\n";

Make sure the Perl executable is on your path.

Now type the following command at your command prompt:

perl hello.pl

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

Hello World

If not, troubleshoot.

Hello.cgi


NOTE! CGI can vary from server to server. Use it at your own risk. It may or may not work on your system. Please feel free to contact me with any problems or suggestions.

On your local PC, save a file called hello.cgi with these lines of code:

#!/usr/local/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. On your pc, make sure the Perl executable is on your path.

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>

If not, troubleshoot.

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, troubleshoot.

Transfer hello.cgi to the server, and set it as executable. chmod a+x hello.cgi should do it. Now, on the server, type this command:

perl ./hello.cgi

Your output should look like this:

Content-type: text/html

<H1>Hello World</H1>

If it doesn't, check to see if the system administrator has pathed you correctly for perl. Try this:

perl -v

Note the output it produces, and call the sysadmin.

Once you get the perl ./hello.cgi command to work correctly, try this:

./hello.cgi

Once again, you should get this output:

Your output should look like this:

Content-type: text/html

<H1>Hello World</H1>

If you don't, use the command which perl to see where perl resides on the ISP's machine, and change the top line of hello.cgi accordingly. If that doesn't fix it, troubleshoot.

Once you get the right output typing just the filename, create this hello.htm in the same 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 with a screen saying Hello World in large letters. Otherwise, troubleshoot.

HelloForm.cgi

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 World.\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 pairs & delimited
#print each ctrl/value pair

Now make it executable with chmod:

chmod a+x HelloForm.cgi

Now put the following form in a web page in the 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>

Access the web page, fill in the text line, click the button, and you'll see the results come up.


 [ Troubleshooters.com | Code Corner | Email Steve Litt ]

Copyright (C)1998 by Steve Litt -- Legal