Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32 |
2 | .\" | |
3 | .\" Standard preamble: | |
4 | .\" ======================================================================== | |
5 | .de Sh \" Subsection heading | |
6 | .br | |
7 | .if t .Sp | |
8 | .ne 5 | |
9 | .PP | |
10 | \fB\\$1\fR | |
11 | .PP | |
12 | .. | |
13 | .de Sp \" Vertical space (when we can't use .PP) | |
14 | .if t .sp .5v | |
15 | .if n .sp | |
16 | .. | |
17 | .de Vb \" Begin verbatim text | |
18 | .ft CW | |
19 | .nf | |
20 | .ne \\$1 | |
21 | .. | |
22 | .de Ve \" End verbatim text | |
23 | .ft R | |
24 | .fi | |
25 | .. | |
26 | .\" Set up some character translations and predefined strings. \*(-- will | |
27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left | |
28 | .\" double quote, and \*(R" will give a right double quote. | will give a | |
29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to | |
30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' | |
31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. | |
32 | .tr \(*W-|\(bv\*(Tr | |
33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' | |
34 | .ie n \{\ | |
35 | . ds -- \(*W- | |
36 | . ds PI pi | |
37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch | |
38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch | |
39 | . ds L" "" | |
40 | . ds R" "" | |
41 | . ds C` "" | |
42 | . ds C' "" | |
43 | 'br\} | |
44 | .el\{\ | |
45 | . ds -- \|\(em\| | |
46 | . ds PI \(*p | |
47 | . ds L" `` | |
48 | . ds R" '' | |
49 | 'br\} | |
50 | .\" | |
51 | .\" If the F register is turned on, we'll generate index entries on stderr for | |
52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index | |
53 | .\" entries marked with X<> in POD. Of course, you'll have to process the | |
54 | .\" output yourself in some meaningful fashion. | |
55 | .if \nF \{\ | |
56 | . de IX | |
57 | . tm Index:\\$1\t\\n%\t"\\$2" | |
58 | .. | |
59 | . nr % 0 | |
60 | . rr F | |
61 | .\} | |
62 | .\" | |
63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes | |
64 | .\" way too many mistakes in technical documents. | |
65 | .hy 0 | |
66 | .if n .na | |
67 | .\" | |
68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). | |
69 | .\" Fear. Run. Save yourself. No user-serviceable parts. | |
70 | . \" fudge factors for nroff and troff | |
71 | .if n \{\ | |
72 | . ds #H 0 | |
73 | . ds #V .8m | |
74 | . ds #F .3m | |
75 | . ds #[ \f1 | |
76 | . ds #] \fP | |
77 | .\} | |
78 | .if t \{\ | |
79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) | |
80 | . ds #V .6m | |
81 | . ds #F 0 | |
82 | . ds #[ \& | |
83 | . ds #] \& | |
84 | .\} | |
85 | . \" simple accents for nroff and troff | |
86 | .if n \{\ | |
87 | . ds ' \& | |
88 | . ds ` \& | |
89 | . ds ^ \& | |
90 | . ds , \& | |
91 | . ds ~ ~ | |
92 | . ds / | |
93 | .\} | |
94 | .if t \{\ | |
95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" | |
96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' | |
97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' | |
98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' | |
99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' | |
100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' | |
101 | .\} | |
102 | . \" troff and (daisy-wheel) nroff accents | |
103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' | |
104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' | |
105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] | |
106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' | |
107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' | |
108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] | |
109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] | |
110 | .ds ae a\h'-(\w'a'u*4/10)'e | |
111 | .ds Ae A\h'-(\w'A'u*4/10)'E | |
112 | . \" corrections for vroff | |
113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' | |
114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' | |
115 | . \" for low resolution devices (crt and lpr) | |
116 | .if \n(.H>23 .if \n(.V>19 \ | |
117 | \{\ | |
118 | . ds : e | |
119 | . ds 8 ss | |
120 | . ds o a | |
121 | . ds d- d\h'-1'\(ga | |
122 | . ds D- D\h'-1'\(hy | |
123 | . ds th \o'bp' | |
124 | . ds Th \o'LP' | |
125 | . ds ae ae | |
126 | . ds Ae AE | |
127 | .\} | |
128 | .rm #[ #] #H #V #F C | |
129 | .\" ======================================================================== | |
130 | .\" | |
131 | .IX Title "A2P 1" | |
132 | .TH A2P 1 "2005-04-04" "perl v5.8.8" "Perl Programmers Reference Guide" | |
133 | .SH "NAME" | |
134 | a2p \- Awk to Perl translator | |
135 | .SH "SYNOPSIS" | |
136 | .IX Header "SYNOPSIS" | |
137 | \&\fBa2p\fR [\fIoptions\fR] [\fIfilename\fR] | |
138 | .SH "DESCRIPTION" | |
139 | .IX Header "DESCRIPTION" | |
140 | \&\fIA2p\fR takes an awk script specified on the command line (or from | |
141 | standard input) and produces a comparable \fIperl\fR script on the | |
142 | standard output. | |
143 | .Sh "\s-1OPTIONS\s0" | |
144 | .IX Subsection "OPTIONS" | |
145 | Options include: | |
146 | .IP "\fB\-D<number>\fR" 5 | |
147 | .IX Item "-D<number>" | |
148 | sets debugging flags. | |
149 | .IP "\fB\-F<character>\fR" 5 | |
150 | .IX Item "-F<character>" | |
151 | tells a2p that this awk script is always invoked with this \fB\-F\fR | |
152 | switch. | |
153 | .IP "\fB\-n<fieldlist>\fR" 5 | |
154 | .IX Item "-n<fieldlist>" | |
155 | specifies the names of the input fields if input does not have to be | |
156 | split into an array. If you were translating an awk script that | |
157 | processes the password file, you might say: | |
158 | .Sp | |
159 | .Vb 1 | |
160 | \& a2p -7 -nlogin.password.uid.gid.gcos.shell.home | |
161 | .Ve | |
162 | .Sp | |
163 | Any delimiter can be used to separate the field names. | |
164 | .IP "\fB\-<number>\fR" 5 | |
165 | .IX Item "-<number>" | |
166 | causes a2p to assume that input will always have that many fields. | |
167 | .IP "\fB\-o\fR" 5 | |
168 | .IX Item "-o" | |
169 | tells a2p to use old awk behavior. The only current differences are: | |
170 | .RS 5 | |
171 | .IP "*" 5 | |
172 | Old awk always has a line loop, even if there are no line | |
173 | actions, whereas new awk does not. | |
174 | .IP "*" 5 | |
175 | In old awk, sprintf is extremely greedy about its arguments. | |
176 | For example, given the statement | |
177 | .Sp | |
178 | .Vb 1 | |
179 | \& print sprintf(some_args), extra_args; | |
180 | .Ve | |
181 | .Sp | |
182 | old awk considers \fIextra_args\fR to be arguments to \f(CW\*(C`sprintf\*(C'\fR; new awk | |
183 | considers them arguments to \f(CW\*(C`print\*(C'\fR. | |
184 | .RE | |
185 | .RS 5 | |
186 | .RE | |
187 | .ie n .Sh """Considerations""" | |
188 | .el .Sh "``Considerations''" | |
189 | .IX Subsection "Considerations" | |
190 | A2p cannot do as good a job translating as a human would, but it | |
191 | usually does pretty well. There are some areas where you may want to | |
192 | examine the perl script produced and tweak it some. Here are some of | |
193 | them, in no particular order. | |
194 | .PP | |
195 | There is an awk idiom of putting \fIint()\fR around a string expression to | |
196 | force numeric interpretation, even though the argument is always | |
197 | integer anyway. This is generally unneeded in perl, but a2p can't | |
198 | tell if the argument is always going to be integer, so it leaves it | |
199 | in. You may wish to remove it. | |
200 | .PP | |
201 | Perl differentiates numeric comparison from string comparison. Awk | |
202 | has one operator for both that decides at run time which comparison to | |
203 | do. A2p does not try to do a complete job of awk emulation at this | |
204 | point. Instead it guesses which one you want. It's almost always | |
205 | right, but it can be spoofed. All such guesses are marked with the | |
206 | comment "\f(CW\*(C`#???\*(C'\fR". You should go through and check them. You might | |
207 | want to run at least once with the \fB\-w\fR switch to perl, which will | |
208 | warn you if you use == where you should have used eq. | |
209 | .PP | |
210 | Perl does not attempt to emulate the behavior of awk in which | |
211 | nonexistent array elements spring into existence simply by being | |
212 | referenced. If somehow you are relying on this mechanism to create | |
213 | null entries for a subsequent for...in, they won't be there in perl. | |
214 | .PP | |
215 | If a2p makes a split line that assigns to a list of variables that | |
216 | looks like (Fld1, Fld2, Fld3...) you may want to rerun a2p using the | |
217 | \&\fB\-n\fR option mentioned above. This will let you name the fields | |
218 | throughout the script. If it splits to an array instead, the script | |
219 | is probably referring to the number of fields somewhere. | |
220 | .PP | |
221 | The exit statement in awk doesn't necessarily exit; it goes to the \s-1END\s0 | |
222 | block if there is one. Awk scripts that do contortions within the \s-1END\s0 | |
223 | block to bypass the block under such circumstances can be simplified | |
224 | by removing the conditional in the \s-1END\s0 block and just exiting directly | |
225 | from the perl script. | |
226 | .PP | |
227 | Perl has two kinds of array, numerically-indexed and associative. | |
228 | Perl associative arrays are called \*(L"hashes\*(R". Awk arrays are usually | |
229 | translated to hashes, but if you happen to know that the index is | |
230 | always going to be numeric you could change the {...} to [...]. | |
231 | Iteration over a hash is done using the \fIkeys()\fR function, but iteration | |
232 | over an array is \s-1NOT\s0. You might need to modify any loop that iterates | |
233 | over such an array. | |
234 | .PP | |
235 | Awk starts by assuming \s-1OFMT\s0 has the value %.6g. Perl starts by | |
236 | assuming its equivalent, $#, to have the value %.20g. You'll want to | |
237 | set $# explicitly if you use the default value of \s-1OFMT\s0. | |
238 | .PP | |
239 | Near the top of the line loop will be the split operation that is | |
240 | implicit in the awk script. There are times when you can move this | |
241 | down past some conditionals that test the entire record so that the | |
242 | split is not done as often. | |
243 | .PP | |
244 | For aesthetic reasons you may wish to change the array base $[ from 1 | |
245 | back to perl's default of 0, but remember to change all array | |
246 | subscripts \s-1AND\s0 all \fIsubstr()\fR and \fIindex()\fR operations to match. | |
247 | .PP | |
248 | Cute comments that say \*(L"# Here is a workaround because awk is dumb\*(R" | |
249 | are passed through unmodified. | |
250 | .PP | |
251 | Awk scripts are often embedded in a shell script that pipes stuff into | |
252 | and out of awk. Often the shell script wrapper can be incorporated | |
253 | into the perl script, since perl can start up pipes into and out of | |
254 | itself, and can do other things that awk can't do by itself. | |
255 | .PP | |
256 | Scripts that refer to the special variables \s-1RSTART\s0 and \s-1RLENGTH\s0 can | |
257 | often be simplified by referring to the variables $`, $& and $', as | |
258 | long as they are within the scope of the pattern match that sets them. | |
259 | .PP | |
260 | The produced perl script may have subroutines defined to deal with | |
261 | awk's semantics regarding getline and print. Since a2p usually picks | |
262 | correctness over efficiency. it is almost always possible to rewrite | |
263 | such code to be more efficient by discarding the semantic sugar. | |
264 | .PP | |
265 | For efficiency, you may wish to remove the keyword from any return | |
266 | statement that is the last statement executed in a subroutine. A2p | |
267 | catches the most common case, but doesn't analyze embedded blocks for | |
268 | subtler cases. | |
269 | .PP | |
270 | ARGV[0] translates to \f(CW$ARGV0\fR, but ARGV[n] translates to \f(CW$ARGV\fR[$n]. A | |
271 | loop that tries to iterate over ARGV[0] won't find it. | |
272 | .SH "ENVIRONMENT" | |
273 | .IX Header "ENVIRONMENT" | |
274 | A2p uses no environment variables. | |
275 | .SH "AUTHOR" | |
276 | .IX Header "AUTHOR" | |
277 | Larry Wall <\fIlarry@wall.org\fR> | |
278 | .SH "FILES" | |
279 | .IX Header "FILES" | |
280 | .SH "SEE ALSO" | |
281 | .IX Header "SEE ALSO" | |
282 | .Vb 1 | |
283 | \& perl The perl compiler/interpreter | |
284 | .Ve | |
285 | .PP | |
286 | .Vb 1 | |
287 | \& s2p sed to perl translator | |
288 | .Ve | |
289 | .SH "DIAGNOSTICS" | |
290 | .IX Header "DIAGNOSTICS" | |
291 | .SH "BUGS" | |
292 | .IX Header "BUGS" | |
293 | It would be possible to emulate awk's behavior in selecting string | |
294 | versus numeric operations at run time by inspection of the operands, | |
295 | but it would be gross and inefficient. Besides, a2p almost always | |
296 | guesses right. | |
297 | .PP | |
298 | Storage for the awk syntax tree is currently static, and can run out. |