home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / PERL30X.ZIP / A2P.MAN < prev    next >
Text File  |  1991-01-14  |  6KB  |  153 lines

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