Opening, Reading, and Outputting a file to the web.
Previous - Brief overview
This first foray into programming isn't very complicated.
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.
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
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.
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.
[Perl help] [ABAP help] [MySQL help]
[TCP/IP troubleshooting] [HTML help] [Feedback] [Humor]
Advertise on Golden Ink's Georgia Network