Perl program that reads a pipe-delimited file and prints HTML

Here’s the complete source code for a Perl program I wrote a long time ago that reads a pipe-delimited data file that contains a list of URLs, and prints its output as an HTML list, using UL, LI, and anchor tags:


# purpose: generate a list of href hyperlinks from a pipe-delimited database.
#          this program reads from an input file, and writes to standard out (stdout).
# usage:   perl this_program input_file_name > output_file_name

# the first argument is the filename we read from
$file = $ARGV[0];
open(FILE,$file) or die "Could not read from $file, program halting. $!";

# print the opening UL tag
print "<ul>\n";

# loop through the input file
while (<FILE>)
    # work with the current record
    ($title,$url,$desc,$rest) = split(/\|/, $_);
    $desc =~ tr/"//d;
    $desc =~ tr/'//d;

    # print the LI and HREF tags for the current row of data
    print "<li><a class=\"recent_link\" title=\"$desc\" href=\"$url\">$title</a></li>\n";

# print the closing UL tag
print "</ul>\n";

The most important things to know about this Perl script are:

  1. The input filename is provided by the Unix shell script that calls this Perl script. That is, the input filename is passed in as a command-line parameter.
  2. The data file contains at least four pipe-delimited fields. In fact, it has many more than this, but I’m only interested in the the three fields I’ve named $title, $url, and $desc.
  3. The program prints its output to standard output (STDOUT) with print statements.

Sample output

Output from this script looks like this:

<li><a class="recent_link" title="The fossil, nicknamed Ida ..." href="">47 million year old fossil unveiled</a></li>
<li><a class="recent_link" title="The Palm Pre will go on sale on 6 June ..." href="">Palm announces Pre launch dates</a></li>
<li><a class="recent_link" title="Info on the Sherlock Holmes movie." href="">Sherlock Holmes movie info</a></li>

There are certainly much fancier ways of generating HTML output, but this works for me. :)