1A2P(1)                User Contributed Perl Documentation               A2P(1)
2
3
4

NAME

6       a2p - Awk to Perl translator
7

SYNOPSIS

9       a2p [options] [filename]
10

DESCRIPTION

12       A2p takes an awk script specified on the command line (or from standard
13       input) and produces a comparable perl script on the standard output.
14
15   OPTIONS
16       Options include:
17
18       -D<number>
19            sets debugging flags.
20
21       -F<character>
22            tells a2p that this awk script is always invoked with this -F
23            switch.
24
25       -n<fieldlist>
26            specifies the names of the input fields if input does not have to
27            be split into an array.  If you were translating an awk script
28            that processes the password file, you might say:
29
30                    a2p -7 -nlogin.password.uid.gid.gcos.shell.home
31
32            Any delimiter can be used to separate the field names.
33
34       -<number>
35            causes a2p to assume that input will always have that many fields.
36
37       -o   tells a2p to use old awk behavior.  The only current differences
38            are:
39
40            •    Old awk always has a line loop, even if there are no line
41                 actions, whereas new awk does not.
42
43            •    In old awk, sprintf is extremely greedy about its arguments.
44                 For example, given the statement
45
46                         print sprintf(some_args), extra_args;
47
48                 old awk considers extra_args to be arguments to "sprintf";
49                 new awk considers them arguments to "print".
50
51   "Considerations"
52       A2p cannot do as good a job translating as a human would, but it
53       usually does pretty well.  There are some areas where you may want to
54       examine the perl script produced and tweak it some.  Here are some of
55       them, in no particular order.
56
57       There is an awk idiom of putting int() around a string expression to
58       force numeric interpretation, even though the argument is always
59       integer anyway.  This is generally unneeded in perl, but a2p can't tell
60       if the argument is always going to be integer, so it leaves it in.  You
61       may wish to remove it.
62
63       Perl differentiates numeric comparison from string comparison.  Awk has
64       one operator for both that decides at run time which comparison to do.
65       A2p does not try to do a complete job of awk emulation at this point.
66       Instead it guesses which one you want.  It's almost always right, but
67       it can be spoofed.  All such guesses are marked with the comment
68       ""#???"".  You should go through and check them.  You might want to run
69       at least once with the -w switch to perl, which will warn you if you
70       use == where you should have used eq.
71
72       Perl does not attempt to emulate the behavior of awk in which
73       nonexistent array elements spring into existence simply by being
74       referenced.  If somehow you are relying on this mechanism to create
75       null entries for a subsequent for...in, they won't be there in perl.
76
77       If a2p makes a split line that assigns to a list of variables that
78       looks like (Fld1, Fld2, Fld3...) you may want to rerun a2p using the -n
79       option mentioned above.  This will let you name the fields throughout
80       the script.  If it splits to an array instead, the script is probably
81       referring to the number of fields somewhere.
82
83       The exit statement in awk doesn't necessarily exit; it goes to the END
84       block if there is one.  Awk scripts that do contortions within the END
85       block to bypass the block under such circumstances can be simplified by
86       removing the conditional in the END block and just exiting directly
87       from the perl script.
88
89       Perl has two kinds of array, numerically-indexed and associative.  Perl
90       associative arrays are called "hashes".  Awk arrays are usually
91       translated to hashes, but if you happen to know that the index is
92       always going to be numeric you could change the {...} to [...].
93       Iteration over a hash is done using the keys() function, but iteration
94       over an array is NOT.  You might need to modify any loop that iterates
95       over such an array.
96
97       Awk starts by assuming OFMT has the value %.6g.  Perl starts by
98       assuming its equivalent, $#, to have the value %.20g.  You'll want to
99       set $# explicitly if you use the default value of OFMT.
100
101       Near the top of the line loop will be the split operation that is
102       implicit in the awk script.  There are times when you can move this
103       down past some conditionals that test the entire record so that the
104       split is not done as often.
105
106       For aesthetic reasons you may wish to change index variables from being
107       1-based (awk style) to 0-based (Perl style).  Be sure to change all
108       operations the variable is involved in to match.
109
110       Cute comments that say "# Here is a workaround because awk is dumb" are
111       passed through unmodified.
112
113       Awk scripts are often embedded in a shell script that pipes stuff into
114       and out of awk.  Often the shell script wrapper can be incorporated
115       into the perl script, since perl can start up pipes into and out of
116       itself, and can do other things that awk can't do by itself.
117
118       Scripts that refer to the special variables RSTART and RLENGTH can
119       often be simplified by referring to the variables $`, $& and $', as
120       long as they are within the scope of the pattern match that sets them.
121
122       The produced perl script may have subroutines defined to deal with
123       awk's semantics regarding getline and print.  Since a2p usually picks
124       correctness over efficiency.  it is almost always possible to rewrite
125       such code to be more efficient by discarding the semantic sugar.
126
127       For efficiency, you may wish to remove the keyword from any return
128       statement that is the last statement executed in a subroutine.  A2p
129       catches the most common case, but doesn't analyze embedded blocks for
130       subtler cases.
131
132       ARGV[0] translates to $ARGV0, but ARGV[n] translates to $ARGV[$n-1].  A
133       loop that tries to iterate over ARGV[0] won't find it.
134

ENVIRONMENT

136       A2p uses no environment variables.
137

AUTHOR

139       Larry Wall <larry@wall.org>
140

SEE ALSO

142        perl   The perl compiler/interpreter
143
144        s2p    sed to perl translator
145

BUGS

147       It would be possible to emulate awk's behavior in selecting string
148       versus numeric operations at run time by inspection of the operands,
149       but it would be gross and inefficient.  Besides, a2p almost always
150       guesses right.
151
152       Storage for the awk syntax tree is currently static, and can run out.
153
154
155
156perl v5.36.0                      2023-01-19                            A2P(1)
Impressum