CSE 341 -- Assignments -- Perl

HW2 : Translating configuration files with Perl

Due: Wed 19 April 2000

The objective of this assignment is to give you a little experience using Perl to process text files.

Please keep in mind that it's very easy to write extremely obscure Perl; style will be even more important than usual in the grading of this assignment. Be kind to your grader, and he will be kind to you.

Turn in info: As always, follow the turnin guidelines. Remember the "341HW" string in your subject line, please.

Scenario

Your company's decided that XML (a family of text markup languages, similar to HTML) is the way of the future, and that all the old .ini and .conf configuration files that your company's applications use will now be in an XML data format. You need to write a script that your customers can use to translate legacy configuration files.

Your script must accept a number of filenames at the command line and produce properly translated files of the same name with the suffix ".xml" appended.

Specifications

The input files are in the following format:

[Section header]
key1=18
key2=list,of,comma,separated,values

... etc.

The XML format your company wants would translate the above into:

<config> <section name="Section header"> <param name="key1"> <item type="number" value="18"/> </param> <param name="key2"> <item type="string" value="list"/> <item type="string" value="of"/> <item type="string" value="comma"/> <item type="string" value="separated"/> <item type="string" value="values"/> </param> </section> ... etc. </config>

You can probably grasp the configuration formats fairly intuitively, but here's a precise definition of the input format (it sounds complicated, but actually it's quite simple):

The output format is as follows:

Generally, XML data formats are whitespace insensitive, but human beings still occasionally read and edit these files by hand, so make sure you get the indentation right.

If this still isn't clear, see the sample files below for a more extended example.

A sample session

Your script should take a list of filenames at the command prompt and output files with .xml appended to the filename. So, for example, the following:

./script.pl foo.ini bar baz.conf

should result in the creation of three files named foo.ini.xml, bar.xml, and baz.conf.xml. To get an idea how to handle command-line arguments, see the tip on @ARGV below.

Starter code and tips

Here is a starter script. It includes an is_number function with extensive comments. Also, here is a sample input file and the output file that should be generated.

Since most of you come from a C/C++ background, you may be tempted to attack this problem using low-level manipulation, treating strings as character arrays. Resist this impulse; Perl has many very powerful string manipulation tools. Use them.

Some Perl tips:

P.S.: In real XML, you would want to escape all <, >, and & signs in the input, translating them to &lt;, &gt;, and &amp; "escape sequences". We're not going to require you to do this, though it's a pretty trivial extension using Perl's string matching facilities. (Obviously, if you want to implement this extension, we won't take off points.)


Last modified: Mon Apr 10 15:16:10 PDT 2000