Opening, Reading, and Outputting a file to the web.
Perl Tutorial, Part II

Previous - Brief overview


What did we just do?

This first foray into programming isn't very complicated.
Every line in perl ends with a semi-colon. When writing a program and looking for a problem, the first thing to check is that all lines end in a semi-colon.

#!/usr/bin/perl
print "Content-Type: text/html\n\n";
print "Hello,world";


The first line (or shebang) indicates the location of the perl executable. Note that this is the only line that does not end with a semi-colon (";"). The second line tells the web browser the type of information that is coming to the screen (MIME-type) and the final line prints the words "Hello, world".

Now let's do something easy, opening a file. We need tell the program the location of the file and give the open file a name (called a file-handle):


#!/usr/bin/perl
open (FILE,"../text/links.txt");    (1)
@file = <FILE>;                     (2)
close FILE;                         (3)
print "Content-Type: text/html\n\n";(4)
print @file;                        (5)

Now to see this program work you will need a file of links, which I have also provided. To make this program work, save it to a text file and upload it (don't forget to chmod). Download the text file and upload it to your server via ftp. You will need to set the permission on the file to read, which should be the default.

Variables

Remember variables from Mrs. Johnson's math class? Well here is a practical application in which to use them. In beginning algebra we learned about X and Y. Perl uses variables in much the same way. $file and @file are two types of variables that we will be using in this program. $file can be a single number, just like an x in the algebra problems, but it can also contain a string of text. @file is an array (orderly list) of variables.

What this program does

The path to the text file (line 2) can vary. If you don't know the relative path (that's what I used), use the absolute path.
This program opens a text file named links.txt in a directory named text (1) and "inhales" the file (copies each line in the file to an element in the array, @file)(2). The file is closed (3), a MIME-type is sent to the browser (4) and the file contents are sent to the browser.

When you see this in a browser, the output looks like, well, garbage, because it is essentially raw, unprocessed data. To make this output better looking, let's add another step.

#!/usr/bin/perl
open (FILE,"../text/links.txt");
@file = <FILE>;
close FILE;
print "Content-Type: text/html\n\n";
$printfile = join ('<br>',@file;)
print $printfile;

This demonstrates the use of the function join. In perl, a function is a routine that is handled by perl.exe. In this case the join function takes each element of the @file array and joins them together with a <br> at the end of each line.

See it work

Now while this program shows the data in readable format, it doesn't really do anything but that. We want a functional program that processes the data into information. So let's remove the join and add a few lines of meaningful processing.

Turning data into information
Return to Perl Help


[Our portfolio] [A history of Computers and Networks]
[Perl help] [ABAP help] [MySQL help]
[TCP/IP troubleshooting] [HTML help] [Feedback] [Humor]
Advertise on Golden Ink's Georgia Network


In Association with Amazon.com