The post that I made is runnable code. If you download it, it will run "as is". I hope that you did that!
Instead of an actual data file, I used the predefined DATA file handle. The data that is being read is right after the __DATA__ statement. This allows me to make a single post that shows the program, the output, and the input data.
What Perl does is open the .pl program for read and then "seeks" to the beginning of the line right after __DATA__. The DATA file handle is initialized by Perl without me doing anything extra. Pretty cool! As trivia, it is possible to "seek" the DATA file handle to the beginning of the file. This would allow a Perl program to actually "read itself".
To make a "real program", you need to put in something like this:
open FILE, '<', "yourfilename" or "die unable to open read file $!";
Now put FILE everywhere that I used DATA.
The other "trick" that I used was perldoc. Perl has a way of embedding documentation right into the program. There is a utility that generates nicely formatted documentation and HTML pages using certain markup tags. The "=prints" says that what follows is documentation. The "=cut" says "end of documentation". So everything between and including the =prints and =cut tags is skipped by the compiler because it figures that this is program documentation.
Asking questions if you see something that you don't understand is fine. I can't predict in advance what you know or don't know.
|