|
Chapter 8Documenting Perl Scripts Perl/Tk
CONTENTS
|
Warning |
You might have to work with lines 2, 3, and 51 to get the spaces right if you are using different versions of nroff. The groff version of GNU did not work on two machines but worked fine on a Sun with these lines: 'di'; To get the two lines to work properly, I had to introduce a space in the calls to the macros: 'di '; |
You have been warned. The limitation of this method should be obvious by now: It's useful for generating one man page for one source file. In addition, it's too heavily tied to the nroff package. The man page will not be generated on NT machines that do not have the nroff packages installed by default. Obviously, something more generic is needed. This is where the POD format comes in.
The Perl plain old documentation (POD) format is designed to be an easier way to get your Perl files documented. Once you have documented your files in the POD format, you can use a translator program to convert your documents into HTML, LaTeX, or man pages. Nothing really prevents you from writing your own translator program; however, once you convert your documents into HTML, you can use off-the-shelf products to convert them into other word processing formats. For example, the Internet Assistant for Microsoft Word lets you read and convert HTML into a variety of formats.
The POD format lets you introduce some formatting directives into
your source files. Note that the formatting terms in Table 8.1
all begin with an equal sign (=).
Term | Description |
=pod | Begins formatting. The Perl interpreter ignores all text until it sees the =end directive. Only POD-related text is found between the =pod and =end directives. |
=end | Stops formatting. Only POD-related text is found between the =pod and =end directives. |
=head1 | Header level 1. |
=head2 | Header level 2. |
=over N | Starts indentation by moving the text to the right by N columns. By convention, the value of N is 4 to accommodate the translation programs; however, it does not have to be 4. |
=back | Nullifies a previous =over directive. An =over/=back pair is used to print lists of items. |
=item C | Specifies an item to be used between =over/=back pairs. C is a character or number to use as the bulleted item. There must be at least one =item in an =over/=back list. |
An example here will help. In Listing 8.2, a file called tf.pod is constructed to document the man page in POD format.
Listing 8.2. A sample POD file.
0 #!/usr/bin/perl
1 =pod
2 =head1 NAME
3 tf - Test file attributes
4
5 =head1 SYNOPSIS
6
7 Usage:
8
9 tf F<file>
10
11 tf F<directory>
12
13 =head1 DESCRIPTION
14
15 The first thing to rememeber is that text is not formatted in a pod
16 file but rather in the formatter. Paragraphs are left as they are.
17
18 The B<tf> program (notice how tf bold) works on these items:
19
20 =over 4
21
22 =item * Files
23
24 Just file names in your directory tree. The file name could be a
25 regular file, socket, device or a link.
26
27 =item * Directories
28
29 Yes, it'll work on directories too.
30
31 =back
32
33 Ship it!
34
35 =head1 BUGS
36
37 Remember the note about features?
38
39 =head1 Header 1
40
41 This is a header 1
42
43 =head2 Header 2
44
45 This is header 2 in I<Italics>.
46
47 =head2 Another Header 2
48
49 This is header 2 in B<BOLD>.
50
51 Another list with non-bulleted items.
52
53 =over 5
54
55 =item First
56
57 This is the First item.
58
59 =item Second
60
61 This is the Second item.
62
63 =item Third
64
65 This is the Third item.
66
67 =back
68
69 =cut
70 ... the rest of the script will be here ...
Line 1 begins the POD portion, and line 69 is where POD processing is cut. Line 70 is where the executable code would start; that is, right after the line that contains the "=cut" tag. Line 0 is present if this is an executable script and absent if this is only a Perl file. All the tags are separated by a blank line, but this is really unnecessary. In my opinion, the POD documentation is more readable if the tags are separated by blank lines.
Now, look at line 18 in Listing 8.2. The B<text> tag is used here to place text in bold typeface. Several tags exist for formatting text. Table 8.2 lists these tags.
Tag | Description |
B<text> | The text is placed in bold. |
I<text> | The text is placed in italics. |
S<text> | The text contains non-breaking spaces. |
C<code> | A literal code for the formatter. |
L<name> | A link to a man page referred to by name. |
L<name/sec> | A link to a section sec in a man page referred to by name. |
L<name/"sec"> | A link to a section in this man page. |
L<"sec"> | A link to a section in this man page. |
F<file> | A file name. |
X<index> | An indexed entry. |
Z<> | A zero width character. |
In most cases you only wind up using the B<> and I<> tags, as you'll see in the documentation that comes with Perl. Refer to Listing 18.2 to see how some of the formatting codes are used in POD files.
The POD information in a file can be included just about anywhere in a source file, although it's best to place this information either at the top or bottom of the source file. As long as you keep your =pod, =cut, and =over/back pairs matched, you shouldn't run into any problems.
Three filters exist that convert POD formatted documents into three different formats. Here's a list of these filters:
Filter | Description |
pod2html | Used to convert POD files to HTML files |
pod2man | Used to convert POD files to man pages |
pod2latex | Used to convert POD files to LaTeX files |
To run these programs, simply type the command and the filename. For example, to generate HTML files from the POD file shown in Listing 18.2, run this command:
pod2html gnat.pod
You'll find that running the pod2html program on gnat.pod created a file called gnat.html in your directory. The output for Listing 18.2 is shown in Figure 18.2.
Figure 8.2 : HTML output from pod2html
This chapter covered two ways of documenting Perl files: one using man pages and the other using POD documentation. man pages can be embedded in the source file, but they require the use of nroff with the man package. POD files are more generic in that you can use translators to convert from POD to one of three known formats: HTML, man, or LaTeX. In extreme cases, you can even write your own Perl script to decode the POD format and write files in your own format. If you really need to do something elaborate, you might want to consider taking the formatted HTML output from a pod2html program and placing the output in a word processor, such as Microsoft Word, to edit the HTML file directly.