version 1 showing authors affecting page license.
.
Rev |
Author |
# |
Line |
1 |
perry |
1 |
A2P |
|
|
2 |
!!!A2P |
|
|
3 |
NAME |
|
|
4 |
SYNOPSIS |
|
|
5 |
DESCRIPTION |
|
|
6 |
ENVIRONMENT |
|
|
7 |
AUTHOR |
|
|
8 |
FILES |
|
|
9 |
SEE ALSO |
|
|
10 |
DIAGNOSTICS |
|
|
11 |
BUGS |
|
|
12 |
---- |
|
|
13 |
!!NAME |
|
|
14 |
|
|
|
15 |
|
|
|
16 |
a2p - Awk to Perl translator |
|
|
17 |
!!SYNOPSIS |
|
|
18 |
|
|
|
19 |
|
|
|
20 |
__a2p [[options] filename__ |
|
|
21 |
!!DESCRIPTION |
|
|
22 |
|
|
|
23 |
|
|
|
24 |
''A2p'' takes an awk script specified on the command line |
|
|
25 |
(or from standard input) and produces a comparable |
|
|
26 |
''perl'' script on the standard output. |
|
|
27 |
|
|
|
28 |
|
|
|
29 |
__Options__ |
|
|
30 |
|
|
|
31 |
|
|
|
32 |
Options include: |
|
|
33 |
|
|
|
34 |
|
|
|
35 |
__-D__ |
|
|
36 |
|
|
|
37 |
|
|
|
38 |
sets debugging flags. |
|
|
39 |
|
|
|
40 |
|
|
|
41 |
__-F__ |
|
|
42 |
|
|
|
43 |
|
|
|
44 |
tells a2p that this awk script is always invoked with this |
|
|
45 |
__-F__ switch. |
|
|
46 |
|
|
|
47 |
|
|
|
48 |
__-n__ |
|
|
49 |
|
|
|
50 |
|
|
|
51 |
specifies the names of the input fields if input does not |
|
|
52 |
have to be split into an array. If you were translating an |
|
|
53 |
awk script that processes the password file, you might |
|
|
54 |
say: |
|
|
55 |
|
|
|
56 |
|
|
|
57 |
a2p -7 -nlogin.password.uid.gid.gcos.shell.home |
|
|
58 |
Any delimiter can be used to separate the field names. |
|
|
59 |
|
|
|
60 |
|
|
|
61 |
__-__ |
|
|
62 |
|
|
|
63 |
|
|
|
64 |
causes a2p to assume that input will always have that many |
|
|
65 |
fields. |
|
|
66 |
|
|
|
67 |
|
|
|
68 |
__-o__ |
|
|
69 |
|
|
|
70 |
|
|
|
71 |
tells a2p to use old awk behavior. The only current |
|
|
72 |
differences are: |
|
|
73 |
|
|
|
74 |
|
|
|
75 |
Old awk always has a line loop, even if there are no line |
|
|
76 |
actions, whereas new awk does not. |
|
|
77 |
|
|
|
78 |
|
|
|
79 |
In old awk, sprintf is extremely greedy about its arguments. |
|
|
80 |
For example, given the statement |
|
|
81 |
|
|
|
82 |
|
|
|
83 |
print sprintf(some_args), extra_args; |
|
|
84 |
old awk considers ''extra_args'' to be arguments to sprintf; new awk considers them arguments to print. |
|
|
85 |
|
|
|
86 |
|
|
|
87 |
__``Considerations''__ |
|
|
88 |
|
|
|
89 |
|
|
|
90 |
A2p cannot do as good a job translating as a human would, |
|
|
91 |
but it usually does pretty well. There are some areas where |
|
|
92 |
you may want to examine the perl script produced and tweak |
|
|
93 |
it some. Here are some of them, in no particular |
|
|
94 |
order. |
|
|
95 |
|
|
|
96 |
|
|
|
97 |
There is an awk idiom of putting ''int()'' around a |
|
|
98 |
string expression to force numeric interpretation, even |
|
|
99 |
though the argument is always integer anyway. This is |
|
|
100 |
generally unneeded in perl, but a2p can't tell if the |
|
|
101 |
argument is always going to be integer, so it leaves it in. |
|
|
102 |
You may wish to remove it. |
|
|
103 |
|
|
|
104 |
|
|
|
105 |
Perl differentiates numeric comparison from string |
|
|
106 |
comparison. Awk has one operator for both that decides at |
|
|
107 |
run time which comparison to do. A2p does not try to do a |
|
|
108 |
complete job of awk emulation at this point. Instead it |
|
|
109 |
guesses which one you want. It's almost always right, but it |
|
|
110 |
can be spoofed. All such guesses are marked with the comment |
|
|
111 |
#??? |
|
|
112 |
-w__ |
|
|
113 |
switch to perl, which will warn you if you use == where you |
|
|
114 |
should have used eq. |
|
|
115 |
|
|
|
116 |
|
|
|
117 |
Perl does not attempt to emulate the behavior of awk in |
|
|
118 |
which nonexistent array elements spring into existence |
|
|
119 |
simply by being referenced. If somehow you are relying on |
|
|
120 |
this mechanism to create null entries for a subsequent |
|
|
121 |
for...in, they won't be there in perl. |
|
|
122 |
|
|
|
123 |
|
|
|
124 |
If a2p makes a split line that assigns to a list of |
|
|
125 |
variables that looks like (Fld1, Fld2, Fld3...) you may want |
|
|
126 |
to rerun a2p using the __-n__ option mentioned above. |
|
|
127 |
This will let you name the fields throughout the script. If |
|
|
128 |
it splits to an array instead, the script is probably |
|
|
129 |
referring to the number of fields somewhere. |
|
|
130 |
|
|
|
131 |
|
|
|
132 |
The exit statement in awk doesn't necessarily exit; it goes |
|
|
133 |
to the END block if there is one. Awk scripts |
|
|
134 |
that do contortions within the END block to |
|
|
135 |
bypass the block under such circumstances can be simplified |
|
|
136 |
by removing the conditional in the END block |
|
|
137 |
and just exiting directly from the perl script. |
|
|
138 |
|
|
|
139 |
|
|
|
140 |
Perl has two kinds of array, numerically-indexed and |
|
|
141 |
associative. Perl associative arrays are called ``hashes''. |
|
|
142 |
Awk arrays are usually translated to hashes, but if you |
|
|
143 |
happen to know that the index is always going to be numeric |
|
|
144 |
you could change the {...} to [[...]. Iteration over a hash |
|
|
145 |
is done using the ''keys()'' function, but iteration over |
|
|
146 |
an array is NOT . You might need to modify |
|
|
147 |
any loop that iterates over such an array. |
|
|
148 |
|
|
|
149 |
|
|
|
150 |
Awk starts by assuming OFMT has the value |
|
|
151 |
%.6g. Perl starts by assuming its equivalent, $#, to have |
|
|
152 |
the value %.20g. You'll want to set $# explicitly if you use |
|
|
153 |
the default value of OFMT . |
|
|
154 |
|
|
|
155 |
|
|
|
156 |
Near the top of the line loop will be the split operation |
|
|
157 |
that is implicit in the awk script. There are times when you |
|
|
158 |
can move this down past some conditionals that test the |
|
|
159 |
entire record so that the split is not done as |
|
|
160 |
often. |
|
|
161 |
|
|
|
162 |
|
|
|
163 |
For aesthetic reasons you may wish to change the array base |
|
|
164 |
$[[ from 1 back to perl's default of 0, but remember to |
|
|
165 |
change all array subscripts AND all |
|
|
166 |
''substr()'' and ''index()'' operations to |
|
|
167 |
match. |
|
|
168 |
|
|
|
169 |
|
|
|
170 |
Cute comments that say ``# Here is a workaround because awk |
|
|
171 |
is dumb'' are passed through unmodified. |
|
|
172 |
|
|
|
173 |
|
|
|
174 |
Awk scripts are often embedded in a shell script that pipes |
|
|
175 |
stuff into and out of awk. Often the shell script wrapper |
|
|
176 |
can be incorporated into the perl script, since perl can |
|
|
177 |
start up pipes into and out of itself, and can do other |
|
|
178 |
things that awk can't do by itself. |
|
|
179 |
|
|
|
180 |
|
|
|
181 |
Scripts that refer to the special variables |
|
|
182 |
RSTART and RLENGTH can often |
|
|
183 |
be simplified by referring to the variables $`, $ |
|
|
184 |
|
|
|
185 |
|
|
|
186 |
The produced perl script may have subroutines defined to |
|
|
187 |
deal with awk's semantics regarding getline and print. Since |
|
|
188 |
a2p usually picks correctness over efficiency. it is almost |
|
|
189 |
always possible to rewrite such code to be more efficient by |
|
|
190 |
discarding the semantic sugar. |
|
|
191 |
|
|
|
192 |
|
|
|
193 |
For efficiency, you may wish to remove the keyword from any |
|
|
194 |
return statement that is the last statement executed in a |
|
|
195 |
subroutine. A2p catches the most common case, but doesn't |
|
|
196 |
analyze embedded blocks for subtler cases. |
|
|
197 |
|
|
|
198 |
|
|
|
199 |
ARGV[[0] translates to $ARGV0, but ARGV[[n] |
|
|
200 |
translates to $ARGV[[$n]. A loop that tries to |
|
|
201 |
iterate over ARGV[[0] won't find it. |
|
|
202 |
!!ENVIRONMENT |
|
|
203 |
|
|
|
204 |
|
|
|
205 |
A2p uses no environment variables. |
|
|
206 |
!!AUTHOR |
|
|
207 |
|
|
|
208 |
|
|
|
209 |
Larry Wall larry@wall.org'''' |
|
|
210 |
!!FILES |
|
|
211 |
!!SEE ALSO |
|
|
212 |
|
|
|
213 |
|
|
|
214 |
perl The perl compiler/interpreter |
|
|
215 |
s2p sed to perl translator |
|
|
216 |
!!DIAGNOSTICS |
|
|
217 |
!!BUGS |
|
|
218 |
|
|
|
219 |
|
|
|
220 |
It would be possible to emulate awk's behavior in selecting |
|
|
221 |
string versus numeric operations at run time by inspection |
|
|
222 |
of the operands, but it would be gross and inefficient. |
|
|
223 |
Besides, a2p almost always guesses right. |
|
|
224 |
|
|
|
225 |
|
|
|
226 |
Storage for the awk syntax tree is currently static, and can |
|
|
227 |
run out. |
|
|
228 |
---- |