a "replacement" for awk and sed
[p5sagit/p5-mst-13.2.git] / x2p / a2p.man
1 .rn '' }`
2 ''' $Header: a2p.man,v 1.0 87/12/18 17:23:56 root Exp $
3 ''' 
4 ''' $Log:       a2p.man,v $
5 ''' Revision 1.0  87/12/18  17:23:56  root
6 ''' Initial revision
7 ''' 
8 ''' 
9 .de Sh
10 .br
11 .ne 5
12 .PP
13 \fB\\$1\fR
14 .PP
15 ..
16 .de Sp
17 .if t .sp .5v
18 .if n .sp
19 ..
20 .de Ip
21 .br
22 .ie \\n.$>=3 .ne \\$3
23 .el .ne 3
24 .IP "\\$1" \\$2
25 ..
26 '''
27 '''     Set up \*(-- to give an unbreakable dash;
28 '''     string Tr holds user defined translation string.
29 '''     Bell System Logo is used as a dummy character.
30 '''
31 .tr \(bs-|\(bv\*(Tr
32 .ie n \{\
33 .ds -- \(bs-
34 .if (\n(.H=4u)&(1m=24u) .ds -- \(bs\h'-12u'\(bs\h'-12u'-\" diablo 10 pitch
35 .if (\n(.H=4u)&(1m=20u) .ds -- \(bs\h'-12u'\(bs\h'-8u'-\" diablo 12 pitch
36 .ds L" ""
37 .ds R" ""
38 .ds L' '
39 .ds R' '
40 'br\}
41 .el\{\
42 .ds -- \(em\|
43 .tr \*(Tr
44 .ds L" ``
45 .ds R" ''
46 .ds L' `
47 .ds R' '
48 'br\}
49 .TH A2P 1 LOCAL
50 .SH NAME
51 a2p - Awk to Perl translator
52 .SH SYNOPSIS
53 .B a2p [options] filename
54 .SH DESCRIPTION
55 .I A2p
56 takes an awk script specified on the command line (or from standard input)
57 and produces a comparable
58 .I perl
59 script on the standard output.
60 .Sh "Options"
61 Options include:
62 .TP 5
63 .B \-D<number>
64 sets debugging flags.
65 .TP 5
66 .B \-F<character>
67 tells a2p that this awk script is always invoked with this -F switch.
68 .TP 5
69 .B \-n<fieldlist>
70 specifies the names of the input fields if input does not have to be split into
71 an array.
72 If you were translating an awk script that processes the password file, you
73 might say:
74 .sp
75         a2p -7 -nlogin.password.uid.gid.gcos.shell.home
76 .sp
77 Any delimiter will do to separate the field names.
78 .TP 5
79 .B \-<number>
80 causes a2p to assume that input will always have that many fields.
81 .Sh "Considerations"
82 A2p cannot do as good a job translating as a human would, but it usually
83 does pretty well.
84 There are some areas where you may want to examine the perl script produced
85 and tweak it some.
86 Here are some of them, in no particular order.
87 .PP
88 The split operator in perl always strips off all null fields from the end.
89 Awk does NOT do this, if you've set FS.
90 If the perl script splits to an array, the field count may not reflect
91 what you expect.
92 Ordinarily this isn't a problem, since nonexistent array elements have a null
93 value, but if you rely on NF in awk, you could be in for trouble.
94 Either force the number of fields with \-<number>, or count the number of
95 delimiters another way, e.g. with y/:/:/.
96 Or add something non-null to the end before you split, and then pop it off
97 the resulting array.
98 .PP
99 There is an awk idiom of putting int() around a string expression to force
100 numeric interpretation, even though the argument is always integer anyway.
101 This is generally unneeded in perl, but a2p can't tell if the argument
102 is always going to be integer, so it leaves it in.
103 You may wish to remove it.
104 .PP
105 Perl differentiates numeric comparison from string comparison.
106 Awk has one operator for both that decides at run time which comparison
107 to do.
108 A2p does not try to do a complete job of awk emulation at this point.
109 Instead it guesses which one you want.
110 It's almost always right, but it can be spoofed.
111 All such guesses are marked with the comment \*(L"#???\*(R".
112 You should go through and check them.
113 .PP
114 Perl does not attempt to emulate the behavior of awk in which nonexistent
115 array elements spring into existence simply by being referenced.
116 If somehow you are relying on this mechanism to create null entries for
117 a subsequent for...in, they won't be there in perl.
118 .PP
119 If a2p makes a split line that assigns to a list of variables that looks
120 like (Fld1, Fld2, Fld3...) you may want
121 to rerun a2p using the \-n option mentioned above.
122 This will let you name the fields throughout the script.
123 If it splits to an array instead, the script is probably referring to the number
124 of fields somewhere.
125 .PP
126 The exit statement in awk doesn't necessarily exit; it goes to the END
127 block if there is one.
128 Awk scripts that do contortions within the END block to bypass the block under
129 such circumstances can be simplified by removing the conditional
130 in the END block and just exiting directly from the perl script.
131 .PP
132 Perl has two kinds of array, numerically-indexed and associative.
133 Awk arrays are usually translated to associative arrays, but if you happen
134 to know that the index is always going to be numeric you could change
135 the {...} to [...].
136 Iteration over an associative array is done with each(), but
137 iteration over a numeric array is NOT.
138 You need a for loop, or while loop with a pop() or shift(), so you might
139 need to modify any loop that is iterating over the array in question.
140 .PP
141 Arrays which have been split into are assumed to be numerically indexed.
142 The usual perl idiom for iterating over such arrays is to use pop() or shift()
143 and assign the resulting value to a variable inside the conditional of the
144 while loop.
145 This is destructive to the array, however, so a2p can't assume this is
146 reasonable.
147 A2p will write a standard for loop with a scratch variable.
148 You may wish to change it to a pop() loop for more efficiency, presuming
149 you don't want to keep the array around.
150 .PP
151 Awk starts by assuming OFMT has the value %.6g.
152 Perl starts by assuming its equivalent, $#, to have the value %.20g.
153 You'll want to set $# explicitly if you use the default value of OFMT.
154 .PP
155 Near the top of the line loop will be the split operation that is implicit in
156 the awk script.
157 There are times when you can move this down past some conditionals that
158 test the entire record so that the split is not done as often.
159 .PP
160 There may occasionally be extra parentheses that you can remove.
161 .PP
162 For aesthetic reasons you may wish to change the array base $[ from 1 back
163 to the default of 0, but remember to change all array subscripts AND
164 all substr() and index() operations to match.
165 .PP
166 Cute comments that say "# Here is a workaround because awk is dumb" are not
167 translated.
168 .PP
169 Awk scripts are often embedded in a shell script that pipes stuff into and
170 out of awk.
171 Often the shell script wrapper can be incorporated into the perl script, since
172 perl can start up pipes into and out of itself, and can do other things that
173 awk can't do by itself.
174 .SH ENVIRONMENT
175 A2p uses no environment variables.
176 .SH AUTHOR
177 Larry Wall <lwall@devvax.Jpl.Nasa.Gov>
178 .SH FILES
179 .SH SEE ALSO
180 perl    The perl compiler/interpreter
181 .br
182 s2p     sed to perl translator
183 .SH DIAGNOSTICS
184 .SH BUGS
185 It would be possible to emulate awk's behavior in selecting string versus
186 numeric operations at run time by inspection of the operands, but it would
187 be gross and inefficient.
188 Besides, a2p almost always guesses right.
189 .PP
190 Storage for the awk syntax tree is currently static, and can run out.
191 .rn }` ''