home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perlkt40.zip / A2P.1 next >
Text File  |  1996-06-14  |  7KB  |  199 lines

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