home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Programming / perlman / man / perltrap.txt < prev    next >
Encoding:
Text File  |  1999-09-09  |  13.7 KB  |  393 lines

  1. NAME
  2.        perltrap - Perl traps for the unwary
  3.  
  4. DESCRIPTION
  5.        The biggest trap of all is forgetting to use the -w
  6.        switch; see the perlrun manpage.  The second biggest trap
  7.        is not making your entire program runnable under use
  8.        strict.
  9.  
  10.        Awk Traps
  11.  
  12.        Accustomed awk users should take special note of the
  13.        following:
  14.  
  15.        o   The English module, loaded via
  16.  
  17.                use English;
  18.  
  19.            allows you to refer to special variables (like $RS) as
  20.            though they were in awk; see the perlvar manpage for
  21.            details.
  22.  
  23.        o   Semicolons are required after all simple statements in
  24.            Perl (except at the end of a block).  Newline is not a
  25.            statement delimiter.
  26.  
  27.        o   Curly brackets are required on ifs and whiles.
  28.  
  29.        o   Variables begin with "$" or "@" in Perl.
  30.  
  31.        o   Arrays index from 0.  Likewise string positions in
  32.            substr() and index().
  33.  
  34.        o   You have to decide whether your array has numeric or
  35.            string indices.
  36.  
  37.        o   Associative array values do not spring into existence
  38.            upon mere reference.
  39.  
  40.        o   You have to decide whether you want to use string or
  41.            numeric comparisons.
  42.  
  43.        o   Reading an input line does not split it for you.  You
  44.            get to split it yourself to an array.  And split()
  45.            operator has different arguments.
  46.  
  47.        o   The current input line is normally in $_, not $0.  It
  48.            generally does not have the newline stripped.  ($0 is
  49.            the name of the program executed.)  See the perlvar
  50.            manpage.
  51.  
  52.        o   $<digit> does not refer to fields--it refers to
  53.            substrings matched by the last match pattern.
  54.  
  55.        o   The print() statement does not add field and record
  56.            separators unless you set $, and $..  You can set $OFS
  57.            and $ORS if you're using the English module.
  58.  
  59.        o   You must open your files before you print to them.
  60.  
  61.        o   The range operator is "..", not comma.  The comma
  62.            operator works as in C.
  63.  
  64.        o   The match operator is "=~", not "~".  ("~" is the
  65.            one's complement operator, as in C.)
  66.  
  67.        o   The exponentiation operator is "**", not "^".  "^" is
  68.            the XOR operator, as in C.  (You know, one could get
  69.            the feeling that awk is basically incompatible with
  70.            C.)
  71.  
  72.        o   The concatenation operator is ".", not the null
  73.            string.  (Using the null string would render /pat/
  74.            /pat/ unparsable, since the third slash would be
  75.            interpreted as a division operator--the tokener is in
  76.            fact slightly context sensitive for operators like
  77.            "/", "?", and ">".  And in fact, "." itself can be the
  78.            beginning of a number.)
  79.  
  80.        o   The next, exit, and continue keywords work
  81.            differently.
  82.  
  83.        o   The following variables work differently:
  84.  
  85.                  Awk       Perl
  86.                  ARGC      $#ARGV or scalar @ARGV
  87.                  ARGV[0]   $0
  88.                  FILENAME  $ARGV
  89.                  FNR       $. - something
  90.                  FS        (whatever you like)
  91.                  NF        $#Fld, or some such
  92.                  NR        $.
  93.                  OFMT      $#
  94.                  OFS       $,
  95.                  ORS       $\
  96.                  RLENGTH   length($&)
  97.                  RS        $/
  98.                  RSTART    length($`)
  99.                  SUBSEP    $;
  100.  
  101.  
  102.        o   You cannot set $RS to a pattern, only a string.
  103.  
  104.        o   When in doubt, run the awk construct through a2p and
  105.            see what it gives you.
  106.  
  107.  
  108.  
  109.        C Traps
  110.  
  111.        Cerebral C programmers should take note of the following:
  112.  
  113.        o   Curly brackets are required on if's and while's.
  114.  
  115.        o   You must use elsif rather than else if.
  116.  
  117.        o   The break and continue keywords from C become in Perl
  118.            last and next, respectively.  Unlike in C, these do
  119.            NOT work within a do { } while construct.
  120.  
  121.        o   There's no switch statement.  (But it's easy to build
  122.            one on the fly.)
  123.  
  124.        o   Variables begin with "$" or "@" in Perl.
  125.  
  126.        o   printf() does not implement the "*" format for
  127.            interpolating field widths, but it's trivial to use
  128.            interpolation of double-quoted strings to achieve the
  129.            same effect.
  130.  
  131.        o   Comments begin with "#", not "/*".
  132.  
  133.        o   You can't take the address of anything, although a
  134.            similar operator in Perl 5 is the backslash, which
  135.            creates a reference.
  136.  
  137.        o   ARGV must be capitalized.  $ARGV[0] is C's argv[1],
  138.            and argv[0] ends up in $0.
  139.  
  140.        o   System calls such as link(), unlink(), rename(), etc.
  141.            return nonzero for success, not 0.
  142.  
  143.        o   Signal handlers deal with signal names, not numbers.
  144.            Use kill -l to find their names on your system.
  145.  
  146.        Sed Traps
  147.  
  148.        Seasoned sed programmers should take note of the
  149.        following:
  150.  
  151.        o   Backreferences in substitutions use "$" rather than
  152.            "\".
  153.  
  154.        o   The pattern matching metacharacters "(", ")", and "|"
  155.            do not have backslashes in front.
  156.  
  157.        o   The range operator is ..., rather than comma.
  158.  
  159.        Shell Traps
  160.  
  161.        Sharp shell programmers should take note of the following:
  162.  
  163.        o   The backtick operator does variable interpretation
  164.            without regard to the presence of single quotes in the
  165.            command.
  166.  
  167.        o   The backtick operator does no translation of the
  168.            return value, unlike csh.
  169.  
  170.        o   Shells (especially csh) do several levels of
  171.            substitution on each command line.  Perl does
  172.            substitution only in certain constructs such as double
  173.            quotes, backticks, angle brackets, and search
  174.            patterns.
  175.  
  176.        o   Shells interpret scripts a little bit at a time.  Perl
  177.            compiles the entire program before executing it
  178.            (except for BEGIN blocks, which execute at compile
  179.            time).
  180.  
  181.        o   The arguments are available via @ARGV, not $1, $2,
  182.            etc.
  183.  
  184.        o   The environment is not automatically made available as
  185.            separate scalar variables.
  186.  
  187.        Perl Traps
  188.  
  189.        Practicing Perl Programmers should take note of the
  190.        following:
  191.  
  192.        o   Remember that many operations behave differently in a
  193.            list context than they do in a scalar one.  See the
  194.            perldata manpage for details.
  195.  
  196.        o   Avoid barewords if you can, especially all lower-case
  197.            ones.  You can't tell just by looking at it whether a
  198.            bareword is a function or a string.  By using quotes
  199.            on strings and parens on function calls, you won't
  200.            ever get them confused.
  201.  
  202.        o   You cannot discern from mere inspection which built-
  203.            ins are unary operators (like chop() and chdir()) and
  204.            which are list operators (like print() and unlink()).
  205.            (User-defined subroutines can only be list operators,
  206.            never unary ones.)  See the perlop manpage.
  207.  
  208.        o   People have a hard time remembering that some
  209.            functions default to $_, or @ARGV, or whatever, but
  210.            that others which you might expect to do not.
  211.  
  212.        o   The <FH> construct is not the name of the filehandle,
  213.            it is a readline operation on that handle.  The data
  214.            read is only assigned to $_ if the file read is the
  215.            sole condition in a while loop:
  216.  
  217.                while (<FH>)      { }
  218.                while ($_ = <FH>) { }..
  219.                <FH>;  # data discarded!
  220.  
  221.  
  222.        o   Remember not to use "=" when you need "=~"; these two
  223.            constructs are quite different:
  224.  
  225.                $x =  /foo/;
  226.                $x =~ /foo/;
  227.  
  228.  
  229.        o   The do {} construct isn't a real loop that you can use
  230.            loop control on.
  231.  
  232.        o   Use my() for local variables whenever you can get away
  233.            with it (but see the perlform manpage for where you
  234.            can't).  Using local() actually gives a local value to
  235.            a global variable, which leaves you open to unforeseen
  236.            side-effects of dynamic scoping.
  237.  
  238.        o   If you localize an exported variable in a module, its
  239.            exported value will not change.  The local name
  240.            becomes an alias to a new value but the external name
  241.            is still an alias for the original.
  242.  
  243.        Perl4 Traps
  244.  
  245.        Penitent Perl 4 Programmers should take note of the
  246.        following incompatible changes that occurred between
  247.        release 4 and release 5:
  248.  
  249.        o   @ now always interpolates an array in double-quotish
  250.            strings.  Some programs may now need to use backslash
  251.            to protect any @ that shouldn't interpolate.
  252.  
  253.        o   Barewords that used to look like strings to Perl will
  254.            now look like subroutine calls if a subroutine by that
  255.            name is defined before the compiler sees them.  For
  256.            example:
  257.  
  258.                sub SeeYa { die "Hasta la vista, baby!" }
  259.                $SIG{'QUIT'} = SeeYa;
  260.  
  261.            In Perl 4, that set the signal handler; in Perl 5, it
  262.            actually calls the function!  You may use the -w
  263.            switch to find such places.
  264.  
  265.        o   Symbols starting with _ are no longer forced into
  266.            package main, except for $_ itself (and @_, etc.).
  267.  
  268.        o   Double-colon is now a valid package separator in an
  269.            identifier.  Thus these behave differently in perl4
  270.            vs. perl5:
  271.                print "$a::$b::$c\n";
  272.                print "$var::abc::xyz\n";
  273.  
  274.  
  275.        o   s'$lhs'$rhs' now does no interpolation on either side.
  276.            It used to interpolate $lhs but not $rhs.
  277.  
  278.        o   The second and third arguments of splice() are now
  279.            evaluated in scalar context (as the book says) rather
  280.            than list context.
  281.  
  282.        o   These are now semantic errors because of precedence:
  283.  
  284.                shift @list + 20;
  285.                $n = keys %map + 20;
  286.  
  287.            Because if that were to work, then this couldn't:
  288.  
  289.                sleep $dormancy + 20;
  290.  
  291.  
  292.        o   The precedence of assignment operators is now the same
  293.            as the precedence of assignment.  Perl 4 mistakenly
  294.            gave them the precedence of the associated operator.
  295.            So you now must parenthesize them in expressions like
  296.  
  297.                /foo/ ? ($a += 2) : ($a -= 2);
  298.  
  299.            Otherwise
  300.  
  301.                /foo/ ? $a += 2 : $a -= 2;
  302.  
  303.            would be erroneously parsed as
  304.  
  305.                (/foo/ ? $a += 2 : $a) -= 2;
  306.  
  307.            On the other hand,
  308.  
  309.                $a += /foo/ ? 1 : 2;
  310.  
  311.            now works as a C programmer would expect.
  312.  
  313.        o   open FOO || die is now incorrect.  You need parens
  314.            around the filehandle.  While temporarily supported,
  315.            using such a construct will generate a non-fatal (but
  316.            non-suppressible) warning.
  317.  
  318.        o   The elements of argument lists for formats are now
  319.            evaluated in list context.  This means you can
  320.            interpolate list values now.
  321.  
  322.        o   You can't do a goto into a block that is optimized
  323.            away.  Darn.
  324.  
  325.        o   It is no longer syntactically legal to use whitespace
  326.            as the name of a variable, or as a delimiter for any
  327.            kind of quote construct.  Double darn.
  328.  
  329.        o   The caller() function now returns a false value in a
  330.            scalar context if there is no caller.  This lets
  331.            library files determine if they're being required.
  332.  
  333.        o   m//g now attaches its state to the searched string
  334.            rather than the regular expression.
  335.  
  336.        o   reverse is no longer allowed as the name of a sort
  337.            subroutine.
  338.  
  339.        o   taintperl is no longer a separate executable.  There
  340.            is now a -T switch to turn on tainting when it isn't
  341.            turned on automatically.
  342.  
  343.        o   Double-quoted strings may no longer end with an
  344.            unescaped $ or @.
  345.  
  346.        o   The archaic while/if BLOCK BLOCK syntax is no longer
  347.            supported.
  348.  
  349.        o   Negative array subscripts now count from the end of
  350.            the array.
  351.  
  352.        o   The comma operator in a scalar context is now
  353.            guaranteed to give a scalar context to its arguments.
  354.  
  355.        o   The ** operator now binds more tightly than unary
  356.            minus.  It was documented to work this way before, but
  357.            didn't.
  358.  
  359.        o   Setting $#array lower now discards array elements.
  360.  
  361.        o   delete() is not guaranteed to return the old value for
  362.            tie()d arrays, since this capability may be onerous
  363.            for some modules to implement.
  364.  
  365.        o   The construct "this is $$x" used to interpolate the
  366.            pid at that point, but now tries to dereference $x.
  367.            $$ by itself still works fine, however.
  368.  
  369.        o   The meaning of foreach has changed slightly when it is
  370.            iterating over a list which is not an array.  This
  371.            used to assign the list to a temporary array, but no
  372.            longer does so (for efficiency).  This means that
  373.            you'll now be iterating over the actual values, not
  374.            over copies of the values.  Modifications to the loop
  375.            variable can change the original values.  To retain
  376.            Perl 4 semantics you need to assign your list
  377.            explicitly to a temporary array and then iterate over
  378.            that.  For example, you might need to change
  379.                foreach $var (grep /x/, @list) { ... }
  380.  
  381.            to
  382.  
  383.                foreach $var (my @tmp = grep /x/, @list) { ... }
  384.  
  385.            Otherwise changing $var will clobber the values of
  386.            @list.  (This most often happens when you use $_ for
  387.            the loop variable, and call subroutines in the loop
  388.            that don't properly localize $_.)
  389.  
  390.        o   Some error messages will be different.
  391.  
  392.        o   Some bugs may have been inadvertently removed.
  393.