1N/A=head1 NAME
1N/A
1N/Aa2p - Awk to Perl translator
1N/A
1N/A=head1 SYNOPSIS
1N/A
1N/AB<a2p [options] filename>
1N/A
1N/A=head1 DESCRIPTION
1N/A
1N/AI<A2p> takes an awk script specified on the command line (or from
1N/Astandard input) and produces a comparable I<perl> script on the
1N/Astandard output.
1N/A
1N/A=head2 Options
1N/A
1N/AOptions include:
1N/A
1N/A=over 5
1N/A
1N/A=item B<-DE<lt>numberE<gt>>
1N/A
1N/Asets debugging flags.
1N/A
1N/A=item B<-FE<lt>characterE<gt>>
1N/A
1N/Atells a2p that this awk script is always invoked with this B<-F>
1N/Aswitch.
1N/A
1N/A=item B<-nE<lt>fieldlistE<gt>>
1N/A
1N/Aspecifies the names of the input fields if input does not have to be
1N/Asplit into an array. If you were translating an awk script that
1N/Aprocesses the password file, you might say:
1N/A
1N/A a2p -7 -nlogin.password.uid.gid.gcos.shell.home
1N/A
1N/AAny delimiter can be used to separate the field names.
1N/A
1N/A=item B<-E<lt>numberE<gt>>
1N/A
1N/Acauses a2p to assume that input will always have that many fields.
1N/A
1N/A=item B<-o>
1N/A
1N/Atells a2p to use old awk behavior. The only current differences are:
1N/A
1N/A=over 5
1N/A
1N/A=item *
1N/A
1N/AOld awk always has a line loop, even if there are no line
1N/Aactions, whereas new awk does not.
1N/A
1N/A=item *
1N/A
1N/AIn old awk, sprintf is extremely greedy about its arguments.
1N/AFor example, given the statement
1N/A
1N/A print sprintf(some_args), extra_args;
1N/A
1N/Aold awk considers I<extra_args> to be arguments to C<sprintf>; new awk
1N/Aconsiders them arguments to C<print>.
1N/A
1N/A=back
1N/A
1N/A=back
1N/A
1N/A=head2 "Considerations"
1N/A
1N/AA2p cannot do as good a job translating as a human would, but it
1N/Ausually does pretty well. There are some areas where you may want to
1N/Aexamine the perl script produced and tweak it some. Here are some of
1N/Athem, in no particular order.
1N/A
1N/AThere is an awk idiom of putting int() around a string expression to
1N/Aforce numeric interpretation, even though the argument is always
1N/Ainteger anyway. This is generally unneeded in perl, but a2p can't
1N/Atell if the argument is always going to be integer, so it leaves it
1N/Ain. You may wish to remove it.
1N/A
1N/APerl differentiates numeric comparison from string comparison. Awk
1N/Ahas one operator for both that decides at run time which comparison to
1N/Ado. A2p does not try to do a complete job of awk emulation at this
1N/Apoint. Instead it guesses which one you want. It's almost always
1N/Aright, but it can be spoofed. All such guesses are marked with the
1N/Acomment "C<#???>". You should go through and check them. You might
1N/Awant to run at least once with the B<-w> switch to perl, which will
1N/Awarn you if you use == where you should have used eq.
1N/A
1N/APerl does not attempt to emulate the behavior of awk in which
1N/Anonexistent array elements spring into existence simply by being
1N/Areferenced. If somehow you are relying on this mechanism to create
1N/Anull entries for a subsequent for...in, they won't be there in perl.
1N/A
1N/AIf a2p makes a split line that assigns to a list of variables that
1N/Alooks like (Fld1, Fld2, Fld3...) you may want to rerun a2p using the
1N/AB<-n> option mentioned above. This will let you name the fields
1N/Athroughout the script. If it splits to an array instead, the script
1N/Ais probably referring to the number of fields somewhere.
1N/A
1N/AThe exit statement in awk doesn't necessarily exit; it goes to the END
1N/Ablock if there is one. Awk scripts that do contortions within the END
1N/Ablock to bypass the block under such circumstances can be simplified
1N/Aby removing the conditional in the END block and just exiting directly
1N/Afrom the perl script.
1N/A
1N/APerl has two kinds of array, numerically-indexed and associative.
1N/APerl associative arrays are called "hashes". Awk arrays are usually
1N/Atranslated to hashes, but if you happen to know that the index is
1N/Aalways going to be numeric you could change the {...} to [...].
1N/AIteration over a hash is done using the keys() function, but iteration
1N/Aover an array is NOT. You might need to modify any loop that iterates
1N/Aover such an array.
1N/A
1N/AAwk starts by assuming OFMT has the value %.6g. Perl starts by
1N/Aassuming its equivalent, $#, to have the value %.20g. You'll want to
1N/Aset $# explicitly if you use the default value of OFMT.
1N/A
1N/ANear the top of the line loop will be the split operation that is
1N/Aimplicit in the awk script. There are times when you can move this
1N/Adown past some conditionals that test the entire record so that the
1N/Asplit is not done as often.
1N/A
1N/AFor aesthetic reasons you may wish to change the array base $[ from 1
1N/Aback to perl's default of 0, but remember to change all array
1N/Asubscripts AND all substr() and index() operations to match.
1N/A
1N/ACute comments that say "# Here is a workaround because awk is dumb"
1N/Aare passed through unmodified.
1N/A
1N/AAwk scripts are often embedded in a shell script that pipes stuff into
1N/Aand out of awk. Often the shell script wrapper can be incorporated
1N/Ainto the perl script, since perl can start up pipes into and out of
1N/Aitself, and can do other things that awk can't do by itself.
1N/A
1N/AScripts that refer to the special variables RSTART and RLENGTH can
1N/Aoften be simplified by referring to the variables $`, $& and $', as
1N/Along as they are within the scope of the pattern match that sets them.
1N/A
1N/AThe produced perl script may have subroutines defined to deal with
1N/Aawk's semantics regarding getline and print. Since a2p usually picks
1N/Acorrectness over efficiency. it is almost always possible to rewrite
1N/Asuch code to be more efficient by discarding the semantic sugar.
1N/A
1N/AFor efficiency, you may wish to remove the keyword from any return
1N/Astatement that is the last statement executed in a subroutine. A2p
1N/Acatches the most common case, but doesn't analyze embedded blocks for
1N/Asubtler cases.
1N/A
1N/AARGV[0] translates to $ARGV0, but ARGV[n] translates to $ARGV[$n]. A
1N/Aloop that tries to iterate over ARGV[0] won't find it.
1N/A
1N/A=head1 ENVIRONMENT
1N/A
1N/AA2p uses no environment variables.
1N/A
1N/A=head1 AUTHOR
1N/A
1N/ALarry Wall E<lt>F<larry@wall.org>E<gt>
1N/A
1N/A=head1 FILES
1N/A
1N/A=head1 SEE ALSO
1N/A
1N/A perl The perl compiler/interpreter
1N/A
1N/A s2p sed to perl translator
1N/A
1N/A=head1 DIAGNOSTICS
1N/A
1N/A=head1 BUGS
1N/A
1N/AIt would be possible to emulate awk's behavior in selecting string
1N/Aversus numeric operations at run time by inspection of the operands,
1N/Abut it would be gross and inefficient. Besides, a2p almost always
1N/Aguesses right.
1N/A
1N/AStorage for the awk syntax tree is currently static, and can run out.