home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / RiscOS / APP / DEVS / PERL / RPC106.ZIP / Rpc106 / Docs / perlop < prev    next >
Text File  |  1997-11-23  |  49KB  |  1,317 lines

  1. NAME
  2.     perlop - Perl operators and precedence
  3.  
  4. SYNOPSIS
  5.     Perl operators have the following associativity and precedence,
  6.     listed from highest precedence to lowest. Note that all
  7.     operators borrowed from C keep the same precedence relationship
  8.     with each other, even where C's precedence is slightly screwy.
  9.     (This makes learning Perl easier for C folks.) With very few
  10.     exceptions, these all operate on scalar values only, not array
  11.     values.
  12.  
  13.     left        terms and list operators (leftward)
  14.     left        ->
  15.     nonassoc    ++ --
  16.     right        **
  17.     right        ! ~ \ and unary + and -
  18.     left        =~ !~
  19.     left        * / % x
  20.     left        + - .
  21.     left        << >>
  22.     nonassoc    named unary operators
  23.     nonassoc    < > <= >= lt gt le ge
  24.     nonassoc    == != <=> eq ne cmp
  25.     left        &
  26.     left        | ^
  27.     left        &&
  28.     left        ||
  29.     nonassoc    ..    ...
  30.     right        ?:
  31.     right        = += -= *= etc.
  32.     left        , =>
  33.     nonassoc    list operators (rightward)
  34.     right        not
  35.     left        and
  36.     left        or xor
  37.  
  38.     In the following sections, these operators are covered in
  39.     precedence order.
  40.  
  41. DESCRIPTION
  42.   Terms and List Operators (Leftward)
  43.  
  44.     A TERM has the highest precedence in Perl. They includes
  45.     variables, quote and quote-like operators, any expression in
  46.     parentheses, and any function whose arguments are parenthesized.
  47.     Actually, there aren't really functions in this sense, just list
  48.     operators and unary operators behaving as functions because you
  49.     put parentheses around the arguments. These are all documented
  50.     in the perlfunc manpage.
  51.  
  52.     If any list operator (print(), etc.) or any unary operator
  53.     (chdir(), etc.) is followed by a left parenthesis as the next
  54.     token, the operator and arguments within parentheses are taken
  55.     to be of highest precedence, just like a normal function call.
  56.  
  57.     In the absence of parentheses, the precedence of list operators
  58.     such as `print', `sort', or `chmod' is either very high or very
  59.     low depending on whether you are looking at the left side or the
  60.     right side of the operator. For example, in
  61.  
  62.     @ary = (1, 3, sort 4, 2);
  63.     print @ary;        # prints 1324
  64.  
  65.     the commas on the right of the sort are evaluated before the
  66.     sort, but the commas on the left are evaluated after. In other
  67.     words, list operators tend to gobble up all the arguments that
  68.     follow them, and then act like a simple TERM with regard to the
  69.     preceding expression. Note that you have to be careful with
  70.     parentheses:
  71.  
  72.     # These evaluate exit before doing the print:
  73.     print($foo, exit);  # Obviously not what you want.
  74.     print $foo, exit;   # Nor is this.
  75.  
  76.     # These do the print before evaluating exit:
  77.     (print $foo), exit; # This is what you want.
  78.     print($foo), exit;  # Or this.
  79.     print ($foo), exit; # Or even this.
  80.  
  81.     Also note that
  82.  
  83.     print ($foo & 255) + 1, "\n";
  84.  
  85.     probably doesn't do what you expect at first glance. See the
  86.     section on "Named Unary Operators" for more discussion of this.
  87.  
  88.     Also parsed as terms are the `do {}' and `eval {}' constructs,
  89.     as well as subroutine and method calls, and the anonymous
  90.     constructors `[]' and `{}'.
  91.  
  92.     See also the section on "Quote and Quote-like Operators" toward
  93.     the end of this section, as well as the section on "I/O
  94.     Operators".
  95.  
  96.   The Arrow Operator
  97.  
  98.     Just as in C and C++, "`->'" is an infix dereference operator.
  99.     If the right side is either a `[...]' or `{...}' subscript, then
  100.     the left side must be either a hard or symbolic reference to an
  101.     array or hash (or a location capable of holding a hard
  102.     reference, if it's an lvalue (assignable)). See the perlref
  103.     manpage.
  104.  
  105.     Otherwise, the right side is a method name or a simple scalar
  106.     variable containing the method name, and the left side must
  107.     either be an object (a blessed reference) or a class name (that
  108.     is, a package name). See the perlobj manpage.
  109.  
  110.   Auto-increment and Auto-decrement
  111.  
  112.     "++" and "--" work as in C. That is, if placed before a
  113.     variable, they increment or decrement the variable before
  114.     returning the value, and if placed after, increment or decrement
  115.     the variable after returning the value.
  116.  
  117.     The auto-increment operator has a little extra builtin magic to
  118.     it. If you increment a variable that is numeric, or that has
  119.     ever been used in a numeric context, you get a normal increment.
  120.     If, however, the variable has been used in only string contexts
  121.     since it was set, and has a value that is not null and matches
  122.     the pattern `/^[a-zA-Z]*[0-9]*$/', the increment is done as a
  123.     string, preserving each character within its range, with carry:
  124.  
  125.     print ++($foo = '99');        # prints '100'
  126.     print ++($foo = 'a0');        # prints 'a1'
  127.     print ++($foo = 'Az');        # prints 'Ba'
  128.     print ++($foo = 'zz');        # prints 'aaa'
  129.  
  130.     The auto-decrement operator is not magical.
  131.  
  132.   Exponentiation
  133.  
  134.     Binary "**" is the exponentiation operator. Note that it binds
  135.     even more tightly than unary minus, so -2**4 is -(2**4), not (-
  136.     2)**4. (This is implemented using C's pow(3) function, which
  137.     actually works on doubles internally.)
  138.  
  139.   Symbolic Unary Operators
  140.  
  141.     Unary "!" performs logical negation, i.e., "not". See also `not'
  142.     for a lower precedence version of this.
  143.  
  144.     Unary "-" performs arithmetic negation if the operand is
  145.     numeric. If the operand is an identifier, a string consisting of
  146.     a minus sign concatenated with the identifier is returned.
  147.     Otherwise, if the string starts with a plus or minus, a string
  148.     starting with the opposite sign is returned. One effect of these
  149.     rules is that `-bareword' is equivalent to `"-bareword"'.
  150.  
  151.     Unary "~" performs bitwise negation, i.e., 1's complement. (See
  152.     also the section on "Integer Arithmetic".)
  153.  
  154.     Unary "+" has no effect whatsoever, even on strings. It is
  155.     useful syntactically for separating a function name from a
  156.     parenthesized expression that would otherwise be interpreted as
  157.     the complete list of function arguments. (See examples above
  158.     under the section on "Terms and List Operators (Leftward)".)
  159.  
  160.     Unary "\" creates a reference to whatever follows it. See the
  161.     perlref manpage. Do not confuse this behavior with the behavior
  162.     of backslash within a string, although both forms do convey the
  163.     notion of protecting the next thing from interpretation.
  164.  
  165.   Binding Operators
  166.  
  167.     Binary "=~" binds a scalar expression to a pattern match.
  168.     Certain operations search or modify the string $_ by default.
  169.     This operator makes that kind of operation work on some other
  170.     string. The right argument is a search pattern, substitution, or
  171.     translation. The left argument is what is supposed to be
  172.     searched, substituted, or translated instead of the default $_.
  173.     The return value indicates the success of the operation. (If the
  174.     right argument is an expression rather than a search pattern,
  175.     substitution, or translation, it is interpreted as a search
  176.     pattern at run time. This can be is less efficient than an
  177.     explicit search, because the pattern must be compiled every time
  178.     the expression is evaluated.
  179.  
  180.     Binary "!~" is just like "=~" except the return value is negated
  181.     in the logical sense.
  182.  
  183.   Multiplicative Operators
  184.  
  185.     Binary "*" multiplies two numbers.
  186.  
  187.     Binary "/" divides two numbers.
  188.  
  189.     Binary "%" computes the modulus of two numbers. Given integer
  190.     operands `$a' and `$b': If `$b' is positive, then `$a % $b' is
  191.     `$a' minus the largest multiple of `$b' that is not greater than
  192.     `$a'. If `$b' is negative, then `$a % $b' is `$a' minus the
  193.     smallest multiple of `$b' that is not less than `$a' (i.e. the
  194.     result will be less than or equal to zero).
  195.  
  196.     Binary "x" is the repetition operator. In a scalar context, it
  197.     returns a string consisting of the left operand repeated the
  198.     number of times specified by the right operand. In a list
  199.     context, if the left operand is a list in parentheses, it
  200.     repeats the list.
  201.  
  202.     print '-' x 80;         # print row of dashes
  203.  
  204.     print "\t" x ($tab/8), ' ' x ($tab%8);        # tab over
  205.  
  206.     @ones = (1) x 80;        # a list of 80 1's
  207.     @ones = (5) x @ones;        # set all elements to 5
  208.  
  209.   Additive Operators
  210.  
  211.     Binary "+" returns the sum of two numbers.
  212.  
  213.     Binary "-" returns the difference of two numbers.
  214.  
  215.     Binary "." concatenates two strings.
  216.  
  217.   Shift Operators
  218.  
  219.     Binary "<<" returns the value of its left argument shifted left
  220.     by the number of bits specified by the right argument. Arguments
  221.     should be integers. (See also the section on "Integer
  222.     Arithmetic".)
  223.  
  224.     Binary ">>" returns the value of its left argument shifted right
  225.     by the number of bits specified by the right argument. Arguments
  226.     should be integers. (See also the section on "Integer
  227.     Arithmetic".)
  228.  
  229.   Named Unary Operators
  230.  
  231.     The various named unary operators are treated as functions with
  232.     one argument, with optional parentheses. These include the
  233.     filetest operators, like `-f', `-M', etc. See the perlfunc
  234.     manpage.
  235.  
  236.     If any list operator (print(), etc.) or any unary operator
  237.     (chdir(), etc.) is followed by a left parenthesis as the next
  238.     token, the operator and arguments within parentheses are taken
  239.     to be of highest precedence, just like a normal function call.
  240.     Examples:
  241.  
  242.     chdir $foo    || die;        # (chdir $foo) || die
  243.     chdir($foo)   || die;        # (chdir $foo) || die
  244.     chdir ($foo)  || die;        # (chdir $foo) || die
  245.     chdir +($foo) || die;        # (chdir $foo) || die
  246.  
  247.     but, because * is higher precedence than ||:
  248.  
  249.     chdir $foo * 20;    # chdir ($foo * 20)
  250.     chdir($foo) * 20;   # (chdir $foo) * 20
  251.     chdir ($foo) * 20;  # (chdir $foo) * 20
  252.     chdir +($foo) * 20; # chdir ($foo * 20)
  253.  
  254.     rand 10 * 20;        # rand (10 * 20)
  255.     rand(10) * 20;        # (rand 10) * 20
  256.     rand (10) * 20;     # (rand 10) * 20
  257.     rand +(10) * 20;    # rand (10 * 20)
  258.  
  259.     See also the section on "Terms and List Operators (Leftward)".
  260.  
  261.   Relational Operators
  262.  
  263.     Binary "<" returns true if the left argument is numerically less
  264.     than the right argument.
  265.  
  266.     Binary ">" returns true if the left argument is numerically
  267.     greater than the right argument.
  268.  
  269.     Binary "<=" returns true if the left argument is numerically
  270.     less than or equal to the right argument.
  271.  
  272.     Binary ">=" returns true if the left argument is numerically
  273.     greater than or equal to the right argument.
  274.  
  275.     Binary "lt" returns true if the left argument is stringwise less
  276.     than the right argument.
  277.  
  278.     Binary "gt" returns true if the left argument is stringwise
  279.     greater than the right argument.
  280.  
  281.     Binary "le" returns true if the left argument is stringwise less
  282.     than or equal to the right argument.
  283.  
  284.     Binary "ge" returns true if the left argument is stringwise
  285.     greater than or equal to the right argument.
  286.  
  287.   Equality Operators
  288.  
  289.     Binary "==" returns true if the left argument is numerically
  290.     equal to the right argument.
  291.  
  292.     Binary "!=" returns true if the left argument is numerically not
  293.     equal to the right argument.
  294.  
  295.     Binary "<=>" returns -1, 0, or 1 depending on whether the left
  296.     argument is numerically less than, equal to, or greater than the
  297.     right argument.
  298.  
  299.     Binary "eq" returns true if the left argument is stringwise
  300.     equal to the right argument.
  301.  
  302.     Binary "ne" returns true if the left argument is stringwise not
  303.     equal to the right argument.
  304.  
  305.     Binary "cmp" returns -1, 0, or 1 depending on whether the left
  306.     argument is stringwise less than, equal to, or greater than the
  307.     right argument.
  308.  
  309.     "lt", "le", "ge", "gt" and "cmp" use the collation (sort) order
  310.     specified by the current locale if `use locale' is in effect.
  311.     See the perllocale manpage.
  312.  
  313.   Bitwise And
  314.  
  315.     Binary "&" returns its operators ANDed together bit by bit. (See
  316.     also the section on "Integer Arithmetic".)
  317.  
  318.   Bitwise Or and Exclusive Or
  319.  
  320.     Binary "|" returns its operators ORed together bit by bit. (See
  321.     also the section on "Integer Arithmetic".)
  322.  
  323.     Binary "^" returns its operators XORed together bit by bit. (See
  324.     also the section on "Integer Arithmetic".)
  325.  
  326.   C-style Logical And
  327.  
  328.     Binary "&&" performs a short-circuit logical AND operation. That
  329.     is, if the left operand is false, the right operand is not even
  330.     evaluated. Scalar or list context propagates down to the right
  331.     operand if it is evaluated.
  332.  
  333.   C-style Logical Or
  334.  
  335.     Binary "||" performs a short-circuit logical OR operation. That
  336.     is, if the left operand is true, the right operand is not even
  337.     evaluated. Scalar or list context propagates down to the right
  338.     operand if it is evaluated.
  339.  
  340.     The `||' and `&&' operators differ from C's in that, rather than
  341.     returning 0 or 1, they return the last value evaluated. Thus, a
  342.     reasonably portable way to find out the home directory (assuming
  343.     it's not "0") might be:
  344.  
  345.     $home = $ENV{'HOME'} || $ENV{'LOGDIR'} ||
  346.         (getpwuid($<))[7] || die "You're homeless!\n";
  347.  
  348.     As more readable alternatives to `&&' and `||', Perl provides
  349.     "and" and "or" operators (see below). The short-circuit behavior
  350.     is identical. The precedence of "and" and "or" is much lower,
  351.     however, so that you can safely use them after a list operator
  352.     without the need for parentheses:
  353.  
  354.     unlink "alpha", "beta", "gamma"
  355.         or gripe(), next LINE;
  356.  
  357.     With the C-style operators that would have been written like
  358.     this:
  359.  
  360.     unlink("alpha", "beta", "gamma")
  361.         || (gripe(), next LINE);
  362.  
  363.   Range Operator
  364.  
  365.     Binary ".." is the range operator, which is really two different
  366.     operators depending on the context. In a list context, it
  367.     returns an array of values counting (by ones) from the left
  368.     value to the right value. This is useful for writing `for
  369.     (1..10)' loops and for doing slice operations on arrays. Be
  370.     aware that under the current implementation, a temporary array
  371.     is created, so you'll burn a lot of memory if you write
  372.     something like this:
  373.  
  374.     for (1 .. 1_000_000) {
  375.         # code
  376.     }
  377.  
  378.     In a scalar context, ".." returns a boolean value. The operator
  379.     is bistable, like a flip-flop, and emulates the line-range
  380.     (comma) operator of sed, awk, and various editors. Each ".."
  381.     operator maintains its own boolean state. It is false as long as
  382.     its left operand is false. Once the left operand is true, the
  383.     range operator stays true until the right operand is true,
  384.     *AFTER* which the range operator becomes false again. (It
  385.     doesn't become false till the next time the range operator is
  386.     evaluated. It can test the right operand and become false on the
  387.     same evaluation it became true (as in awk), but it still returns
  388.     true once. If you don't want it to test the right operand till
  389.     the next evaluation (as in sed), use three dots ("...") instead
  390.     of two.) The right operand is not evaluated while the operator
  391.     is in the "false" state, and the left operand is not evaluated
  392.     while the operator is in the "true" state. The precedence is a
  393.     little lower than || and &&. The value returned is either the
  394.     null string for false, or a sequence number (beginning with 1)
  395.     for true. The sequence number is reset for each range
  396.     encountered. The final sequence number in a range has the string
  397.     "E0" appended to it, which doesn't affect its numeric value, but
  398.     gives you something to search for if you want to exclude the
  399.     endpoint. You can exclude the beginning point by waiting for the
  400.     sequence number to be greater than 1. If either operand of
  401.     scalar ".." is a numeric literal, that operand is implicitly
  402.     compared to the `$.' variable, the current line number.
  403.     Examples:
  404.  
  405.     As a scalar operator:
  406.  
  407.     if (101 .. 200) { print; }  # print 2nd hundred lines
  408.     next line if (1 .. /^$/);   # skip header lines
  409.     s/^/> / if (/^$/ .. eof()); # quote body
  410.  
  411.     As a list operator:
  412.  
  413.     for (101 .. 200) { print; } # print $_ 100 times
  414.     @foo = @foo[0 .. $#foo];    # an expensive no-op
  415.     @foo = @foo[$#foo-4 .. $#foo];        # slice last 5 items
  416.  
  417.     The range operator (in a list context) makes use of the magical
  418.     auto-increment algorithm if the operands are strings. You can
  419.     say
  420.  
  421.     @alphabet = ('A' .. 'Z');
  422.  
  423.     to get all the letters of the alphabet, or
  424.  
  425.     $hexdigit = (0 .. 9, 'a' .. 'f')[$num & 15];
  426.  
  427.     to get a hexadecimal digit, or
  428.  
  429.     @z2 = ('01' .. '31');  print $z2[$mday];
  430.  
  431.     to get dates with leading zeros. If the final value specified is
  432.     not in the sequence that the magical increment would produce,
  433.     the sequence goes until the next value would be longer than the
  434.     final value specified.
  435.  
  436.   Conditional Operator
  437.  
  438.     Ternary "?:" is the conditional operator, just as in C. It works
  439.     much like an if-then-else. If the argument before the ? is true,
  440.     the argument before the : is returned, otherwise the argument
  441.     after the : is returned. For example:
  442.  
  443.     printf "I have %d dog%s.\n", $n,
  444.         ($n == 1) ? '' : "s";
  445.  
  446.     Scalar or list context propagates downward into the 2nd or 3rd
  447.     argument, whichever is selected.
  448.  
  449.     $a = $ok ? $b : $c;  # get a scalar
  450.     @a = $ok ? @b : @c;  # get an array
  451.     $a = $ok ? @b : @c;  # oops, that's just a count!
  452.  
  453.     The operator may be assigned to if both the 2nd and 3rd
  454.     arguments are legal lvalues (meaning that you can assign to
  455.     them):
  456.  
  457.     ($a_or_b ? $a : $b) = $c;
  458.  
  459.     This is not necessarily guaranteed to contribute to the
  460.     readability of your program.
  461.  
  462.   Assignment Operators
  463.  
  464.     "=" is the ordinary assignment operator.
  465.  
  466.     Assignment operators work as in C. That is,
  467.  
  468.     $a += 2;
  469.  
  470.     is equivalent to
  471.  
  472.     $a = $a + 2;
  473.  
  474.     although without duplicating any side effects that dereferencing
  475.     the lvalue might trigger, such as from tie(). Other assignment
  476.     operators work similarly. The following are recognized:
  477.  
  478.     **=    +=    *=    &=     <<=    &&=
  479.            -=    /=    |=     >>=    ||=
  480.            .=    %=    ^=
  481.              x=
  482.  
  483.     Note that while these are grouped by family, they all have the
  484.     precedence of assignment.
  485.  
  486.     Unlike in C, the assignment operator produces a valid lvalue.
  487.     Modifying an assignment is equivalent to doing the assignment
  488.     and then modifying the variable that was assigned to. This is
  489.     useful for modifying a copy of something, like this:
  490.  
  491.     ($tmp = $global) =~ tr [A-Z] [a-z];
  492.  
  493.     Likewise,
  494.  
  495.     ($a += 2) *= 3;
  496.  
  497.     is equivalent to
  498.  
  499.     $a += 2;
  500.     $a *= 3;
  501.  
  502.   Comma Operator
  503.  
  504.     Binary "," is the comma operator. In a scalar context it
  505.     evaluates its left argument, throws that value away, then
  506.     evaluates its right argument and returns that value. This is
  507.     just like C's comma operator.
  508.  
  509.     In a list context, it's just the list argument separator, and
  510.     inserts both its arguments into the list.
  511.  
  512.     The => digraph is mostly just a synonym for the comma operator.
  513.     It's useful for documenting arguments that come in pairs. As of
  514.     release 5.001, it also forces any word to the left of it to be
  515.     interpreted as a string.
  516.  
  517.   List Operators (Rightward)
  518.  
  519.     On the right side of a list operator, it has very low
  520.     precedence, such that it controls all comma-separated
  521.     expressions found there. The only operators with lower
  522.     precedence are the logical operators "and", "or", and "not",
  523.     which may be used to evaluate calls to list operators without
  524.     the need for extra parentheses:
  525.  
  526.     open HANDLE, "filename"
  527.         or die "Can't open: $!\n";
  528.  
  529.     See also discussion of list operators in the section on "Terms
  530.     and List Operators (Leftward)".
  531.  
  532.   Logical Not
  533.  
  534.     Unary "not" returns the logical negation of the expression to
  535.     its right. It's the equivalent of "!" except for the very low
  536.     precedence.
  537.  
  538.   Logical And
  539.  
  540.     Binary "and" returns the logical conjunction of the two
  541.     surrounding expressions. It's equivalent to && except for the
  542.     very low precedence. This means that it short-circuits: i.e.,
  543.     the right expression is evaluated only if the left expression is
  544.     true.
  545.  
  546.   Logical or and Exclusive Or
  547.  
  548.     Binary "or" returns the logical disjunction of the two
  549.     surrounding expressions. It's equivalent to || except for the
  550.     very low precedence. This means that it short-circuits: i.e.,
  551.     the right expression is evaluated only if the left expression is
  552.     false.
  553.  
  554.     Binary "xor" returns the exclusive-OR of the two surrounding
  555.     expressions. It cannot short circuit, of course.
  556.  
  557.   C Operators Missing From Perl
  558.  
  559.     Here is what C has that Perl doesn't:
  560.  
  561.     unary & Address-of operator. (But see the "\" operator for taking a
  562.         reference.)
  563.  
  564.     unary * Dereference-address operator. (Perl's prefix dereferencing
  565.         operators are typed: $, @, %, and &.)
  566.  
  567.     (TYPE)  Type casting operator.
  568.  
  569.   Quote and Quote-like Operators
  570.  
  571.     While we usually think of quotes as literal values, in Perl they
  572.     function as operators, providing various kinds of interpolating
  573.     and pattern matching capabilities. Perl provides customary quote
  574.     characters for these behaviors, but also provides a way for you
  575.     to choose your quote character for any of them. In the following
  576.     table, a `{}' represents any pair of delimiters you choose. Non-
  577.     bracketing delimiters use the same character fore and aft, but
  578.     the 4 sorts of brackets (round, angle, square, curly) will all
  579.     nest.
  580.  
  581.     Customary  Generic     Meaning      Interpolates
  582.         ''         q{}       Literal           no
  583.         ""        qq{}       Literal           yes
  584.         ``        qx{}       Command           yes
  585.             qw{}      Word list        no
  586.         //         m{}    Pattern match      yes
  587.              s{}{}   Substitution      yes
  588.             tr{}{}   Translation       no
  589.  
  590.     Note that there can be whitespace between the operator and the
  591.     quoting characters, except when `#' is being used as the quoting
  592.     character. `q#foo#' is parsed as being the string `foo', which
  593.     `q #foo#' is the operator `q' followed by a comment. Its
  594.     argument will be taken from the next line. This allows you to
  595.     write:
  596.  
  597.     s {foo}  # Replace foo
  598.       {bar}  # with bar.
  599.  
  600.     For constructs that do interpolation, variables beginning with
  601.     "`$'" or "`@'" are interpolated, as are the following sequences:
  602.  
  603.     \t        tab         (HT, TAB)
  604.     \n        newline        (LF, NL)
  605.     \r        return        (CR)
  606.     \f        form feed        (FF)
  607.     \b        backspace        (BS)
  608.     \a        alarm (bell)    (BEL)
  609.     \e        escape        (ESC)
  610.     \033        octal char
  611.     \x1b        hex char
  612.     \c[        control char
  613.     \l        lowercase next char
  614.     \u        uppercase next char
  615.     \L        lowercase till \E
  616.     \U        uppercase till \E
  617.     \E        end case modification
  618.     \Q        quote regexp metacharacters till \E
  619.  
  620.     If `use locale' is in effect, the case map used by `\l', `\L',
  621.     `\u' and <\U> is taken from the current locale. See the
  622.     perllocale manpage.
  623.  
  624.     Patterns are subject to an additional level of interpretation as
  625.     a regular expression. This is done as a second pass, after
  626.     variables are interpolated, so that regular expressions may be
  627.     incorporated into the pattern from the variables. If this is not
  628.     what you want, use `\Q' to interpolate a variable literally.
  629.  
  630.     Apart from the above, there are no multiple levels of
  631.     interpolation. In particular, contrary to the expectations of
  632.     shell programmers, back-quotes do *NOT* interpolate within
  633.     double quotes, nor do single quotes impede evaluation of
  634.     variables when used within double quotes.
  635.  
  636.   Regexp Quote-Like Operators
  637.  
  638.     Here are the quote-like operators that apply to pattern matching
  639.     and related activities.
  640.  
  641.     ?PATTERN?
  642.         This is just like the `/pattern/' search, except that it
  643.         matches only once between calls to the reset() operator.
  644.         This is a useful optimization when you want to see only
  645.         the first occurrence of something in each file of a set
  646.         of files, for instance. Only `??' patterns local to the
  647.         current package are reset.
  648.  
  649.         This usage is vaguely deprecated, and may be removed in
  650.         some future version of Perl.
  651.  
  652.     m/PATTERN/cgimosx
  653.     /PATTERN/cgimosx
  654.         Searches a string for a pattern match, and in a scalar
  655.         context returns true (1) or false (''). If no string is
  656.         specified via the `=~' or `!~' operator, the $_ string
  657.         is searched. (The string specified with `=~' need not be
  658.         an lvalue--it may be the result of an expression
  659.         evaluation, but remember the `=~' binds rather tightly.)
  660.         See also the perlre manpage. See the perllocale manpage
  661.         for discussion of additional considerations which apply
  662.         when `use locale' is in effect.
  663.  
  664.         Options are:
  665.  
  666.         c   Do not reset search position on a failed match when /g is in effect.
  667.         g   Match globally, i.e., find all occurrences.
  668.         i   Do case-insensitive pattern matching.
  669.         m   Treat string as multiple lines.
  670.         o   Compile pattern only once.
  671.         s   Treat string as single line.
  672.         x   Use extended regular expressions.
  673.  
  674.         If "/" is the delimiter then the initial `m' is
  675.         optional. With the `m' you can use any pair of non-
  676.         alphanumeric, non-whitespace characters as delimiters.
  677.         This is particularly useful for matching Unix path names
  678.         that contain "/", to avoid LTS (leaning toothpick
  679.         syndrome). If "?" is the delimiter, then the match-only-
  680.         once rule of `?PATTERN?' applies.
  681.  
  682.         PATTERN may contain variables, which will be
  683.         interpolated (and the pattern recompiled) every time the
  684.         pattern search is evaluated. (Note that `$)' and `$|'
  685.         might not be interpolated because they look like end-of-
  686.         string tests.) If you want such a pattern to be compiled
  687.         only once, add a `/o' after the trailing delimiter. This
  688.         avoids expensive run-time recompilations, and is useful
  689.         when the value you are interpolating won't change over
  690.         the life of the script. However, mentioning `/o'
  691.         constitutes a promise that you won't change the
  692.         variables in the pattern. If you change them, Perl won't
  693.         even notice.
  694.  
  695.         If the PATTERN evaluates to a null string, the last
  696.         successfully executed regular expression is used
  697.         instead.
  698.  
  699.         If used in a context that requires a list value, a
  700.         pattern match returns a list consisting of the
  701.         subexpressions matched by the parentheses in the
  702.         pattern, i.e., (`$1', $2, $3...). (Note that here $1
  703.         etc. are also set, and that this differs from Perl 4's
  704.         behavior.) If the match fails, a null array is returned.
  705.         If the match succeeds, but there were no parentheses, a
  706.         list value of (1) is returned.
  707.  
  708.         Examples:
  709.  
  710.         open(TTY, '/dev/tty');
  711.         <TTY> =~ /^y/i && foo();    # do foo if desired
  712.  
  713.         if (/Version: *([0-9.]*)/) { $version = $1; }
  714.  
  715.         next if m#^/usr/spool/uucp#;
  716.  
  717.         # poor man's grep
  718.         $arg = shift;
  719.         while (<>) {
  720.             print if /$arg/o;        # compile only once
  721.         }
  722.  
  723.         if (($F1, $F2, $Etc) = ($foo =~ /^(\S+)\s+(\S+)\s*(.*)/))
  724.  
  725.         This last example splits $foo into the first two words
  726.         and the remainder of the line, and assigns those three
  727.         fields to $F1, $F2, and $Etc. The conditional is true if
  728.         any variables were assigned, i.e., if the pattern
  729.         matched.
  730.  
  731.         The `/g' modifier specifies global pattern matching--
  732.         that is, matching as many times as possible within the
  733.         string. How it behaves depends on the context. In a list
  734.         context, it returns a list of all the substrings matched
  735.         by all the parentheses in the regular expression. If
  736.         there are no parentheses, it returns a list of all the
  737.         matched strings, as if there were parentheses around the
  738.         whole pattern.
  739.  
  740.         In a scalar context, `m//g' iterates through the string,
  741.         returning TRUE each time it matches, and FALSE when it
  742.         eventually runs out of matches. (In other words, it
  743.         remembers where it left off last time and restarts the
  744.         search at that point. You can actually find the current
  745.         match position of a string or set it using the pos()
  746.         function; see the "pos" entry in the perlfunc manpage.)
  747.         A failed match normally resets the search position to
  748.         the beginning of the string, but you can avoid that by
  749.         adding the `/c' modifier (e.g. `m//gc'). Modifying the
  750.         target string also resets the search position.
  751.  
  752.         You can intermix `m//g' matches with `m/\G.../g', where
  753.         `\G' is a zero-width assertion that matches the exact
  754.         position where the previous `m//g', if any, left off.
  755.         The `\G' assertion is not supported without the `/g'
  756.         modifier; currently, without `/g', `\G' behaves just
  757.         like `\A', but that's accidental and may change in the
  758.         future.
  759.  
  760.         Examples:
  761.  
  762.         # list context
  763.         ($one,$five,$fifteen) = (`uptime` =~ /(\d+\.\d+)/g);
  764.  
  765.         # scalar context
  766.         $/ = ""; $* = 1;  # $* deprecated in modern perls
  767.         while (defined($paragraph = <>)) {
  768.             while ($paragraph =~ /[a-z]['")]*[.!?]+['")]*\s/g) {
  769.             $sentences++;
  770.             }
  771.         }
  772.         print "$sentences\n";
  773.  
  774.         # using m//gc with \G
  775.         $_ = "ppooqppqq";
  776.         while ($i++ < 2) {
  777.             print "1: '";
  778.             print $1 while /(o)/gc; print "', pos=", pos, "\n";
  779.             print "2: '";
  780.             print $1 if /\G(q)/gc;  print "', pos=", pos, "\n";
  781.             print "3: '";
  782.             print $1 while /(p)/gc; print "', pos=", pos, "\n";
  783.         }
  784.  
  785.         The last example should print:
  786.  
  787.         1: 'oo', pos=4
  788.         2: 'q', pos=5
  789.         3: 'pp', pos=7
  790.         1: '', pos=7
  791.         2: 'q', pos=8
  792.         3: '', pos=8
  793.  
  794.         A useful idiom for `lex'-like scanners is `/\G.../gc'.
  795.         You can combine several regexps like this to process a
  796.         string part-by-part, doing different actions depending
  797.         on which regexp matched. Each regexp tries to match
  798.         where the previous one leaves off.
  799.  
  800.          $_ = <<'EOL';
  801.           $url = new URI::URL "http://www/";   die if $url eq "xXx";
  802.          EOL
  803.          LOOP:
  804.         {
  805.           print(" digits"),        redo LOOP if /\G\d+\b[,.;]?\s*/gc;
  806.           print(" lowercase"),        redo LOOP if /\G[a-z]+\b[,.;]?\s*/gc;
  807.           print(" UPPERCASE"),        redo LOOP if /\G[A-Z]+\b[,.;]?\s*/gc;
  808.           print(" Capitalized"),    redo LOOP if /\G[A-Z][a-z]+\b[,.;]?\s*/gc;
  809.           print(" MiXeD"),        redo LOOP if /\G[A-Za-z]+\b[,.;]?\s*/gc;
  810.           print(" alphanumeric"),   redo LOOP if /\G[A-Za-z0-9]+\b[,.;]?\s*/gc;
  811.           print(" line-noise"),     redo LOOP if /\G[^A-Za-z0-9]+/gc;
  812.           print ". That's all!\n";
  813.         }
  814.  
  815.         Here is the output (split into several lines):
  816.  
  817.          line-noise lowercase line-noise lowercase UPPERCASE line-noise
  818.          UPPERCASE line-noise lowercase line-noise lowercase line-noise
  819.          lowercase lowercase line-noise lowercase lowercase line-noise
  820.          MiXeD line-noise. That's all!
  821.  
  822.     q/STRING/
  823.     `'STRING''
  824.         A single-quoted, literal string. A backslash represents
  825.         a backslash unless followed by the delimiter or another
  826.         backslash, in which case the delimiter or backslash is
  827.         interpolated.
  828.  
  829.         $foo = q!I said, "You said, 'She said it.'"!;
  830.         $bar = q('This is it.');
  831.         $baz = '\n';            # a two-character string
  832.  
  833.     qq/STRING/
  834.     "STRING"
  835.         A double-quoted, interpolated string.
  836.  
  837.         $_ .= qq
  838.          (*** The previous line contains the naughty word "$1".\n)
  839.                 if /(tcl|rexx|python)/;     # :-)
  840.         $baz = "\n";            # a one-character string
  841.  
  842.     qx/STRING/
  843.     `STRING`
  844.         A string which is interpolated and then executed as a
  845.         system command. The collected standard output of the
  846.         command is returned. In scalar context, it comes back as
  847.         a single (potentially multi-line) string. In list
  848.         context, returns a list of lines (however you've defined
  849.         lines with $/ or $INPUT_RECORD_SEPARATOR).
  850.  
  851.         $today = qx{ date };
  852.  
  853.         Note that how the string gets evaluated is entirely
  854.         subject to the command interpreter on your system. On
  855.         most platforms, you will have to protect shell
  856.         metacharacters if you want them treated literally. On
  857.         some platforms (notably DOS-like ones), the shell may
  858.         not be capable of dealing with multiline commands, so
  859.         putting newlines in the string may not get you what you
  860.         want. You may be able to evaluate multiple commands in a
  861.         single line by separating them with the command
  862.         separator character, if your shell supports that (e.g.
  863.         `;' on many Unix shells; `&' on the Windows NT `cmd'
  864.         shell).
  865.  
  866.         Beware that some command shells may place restrictions
  867.         on the length of the command line. You must ensure your
  868.         strings don't exceed this limit after any necessary
  869.         interpolations. See the platform-specific release notes
  870.         for more details about your particular environment.
  871.  
  872.         Also realize that using this operator frequently leads
  873.         to unportable programs.
  874.  
  875.         See the section on "I/O Operators" for more discussion.
  876.  
  877.     qw/STRING/
  878.         Returns a list of the words extracted out of STRING,
  879.         using embedded whitespace as the word delimiters. It is
  880.         exactly equivalent to
  881.  
  882.         split(' ', q/STRING/);
  883.  
  884.         Some frequently seen examples:
  885.  
  886.         use POSIX qw( setlocale localeconv )
  887.         @EXPORT = qw( foo bar baz );
  888.  
  889.         A common mistake is to try to separate the words with
  890.         comma or to put comments into a multi-line qw-string.
  891.         For this reason the `-w' switch produce warnings if the
  892.         STRING contains the "," or the "#" character.
  893.  
  894.     s/PATTERN/REPLACEMENT/egimosx
  895.         Searches a string for a pattern, and if found, replaces
  896.         that pattern with the replacement text and returns the
  897.         number of substitutions made. Otherwise it returns false
  898.         (specifically, the empty string).
  899.  
  900.         If no string is specified via the `=~' or `!~' operator,
  901.         the `$_' variable is searched and modified. (The string
  902.         specified with `=~' must be a scalar variable, an array
  903.         element, a hash element, or an assignment to one of
  904.         those, i.e., an lvalue.)
  905.  
  906.         If the delimiter chosen is single quote, no variable
  907.         interpolation is done on either the PATTERN or the
  908.         REPLACEMENT. Otherwise, if the PATTERN contains a $ that
  909.         looks like a variable rather than an end-of-string test,
  910.         the variable will be interpolated into the pattern at
  911.         run-time. If you want the pattern compiled only once the
  912.         first time the variable is interpolated, use the `/o'
  913.         option. If the pattern evaluates to a null string, the
  914.         last successfully executed regular expression is used
  915.         instead. See the perlre manpage for further explanation
  916.         on these. See the perllocale manpage for discussion of
  917.         additional considerations which apply when `use locale'
  918.         is in effect.
  919.  
  920.         Options are:
  921.  
  922.         e   Evaluate the right side as an expression.
  923.         g   Replace globally, i.e., all occurrences.
  924.         i   Do case-insensitive pattern matching.
  925.         m   Treat string as multiple lines.
  926.         o   Compile pattern only once.
  927.         s   Treat string as single line.
  928.         x   Use extended regular expressions.
  929.  
  930.         Any non-alphanumeric, non-whitespace delimiter may
  931.         replace the slashes. If single quotes are used, no
  932.         interpretation is done on the replacement string (the
  933.         `/e' modifier overrides this, however). Unlike Perl 4,
  934.         Perl 5 treats backticks as normal delimiters; the
  935.         replacement text is not evaluated as a command. If the
  936.         PATTERN is delimited by bracketing quotes, the
  937.         REPLACEMENT has its own pair of quotes, which may or may
  938.         not be bracketing quotes, e.g., `s(foo)(bar)' or
  939.         `s<foo>/bar/'. A `/e' will cause the replacement portion
  940.         to be interpreter as a full-fledged Perl expression and
  941.         eval()ed right then and there. It is, however, syntax
  942.         checked at compile-time.
  943.  
  944.         Examples:
  945.  
  946.         s/\bgreen\b/mauve/g;            # don't change wintergreen
  947.  
  948.         $path =~ s|/usr/bin|/usr/local/bin|;
  949.  
  950.         s/Login: $foo/Login: $bar/; # run-time pattern
  951.  
  952.         ($foo = $bar) =~ s/this/that/;
  953.  
  954.         $count = ($paragraph =~ s/Mister\b/Mr./g);
  955.  
  956.         $_ = 'abc123xyz';
  957.         s/\d+/$&*2/e;            # yields 'abc246xyz'
  958.         s/\d+/sprintf("%5d",$&)/e;  # yields 'abc  246xyz'
  959.         s/\w/$& x 2/eg;         # yields 'aabbcc  224466xxyyzz'
  960.  
  961.         s/%(.)/$percent{$1}/g;        # change percent escapes; no /e
  962.         s/%(.)/$percent{$1} || $&/ge;        # expr now, so /e
  963.         s/^=(\w+)/&pod($1)/ge;        # use function call
  964.  
  965.         # /e's can even nest;  this will expand
  966.         # simple embedded variables in $_
  967.         s/(\$\w+)/$1/eeg;
  968.  
  969.         # Delete C comments.
  970.         $program =~ s {
  971.             /\*     # Match the opening delimiter.
  972.             .*?     # Match a minimal number of characters.
  973.             \*/     # Match the closing delimiter.
  974.         } []gsx;
  975.  
  976.         s/^\s*(.*?)\s*$/$1/;        # trim white space
  977.  
  978.         s/([^ ]*) *([^ ]*)/$2 $1/;  # reverse 1st two fields
  979.  
  980.         Note the use of $ instead of \ in the last example.
  981.         Unlike sed, we use the \<*digit*> form in only the left
  982.         hand side. Anywhere else it's $<*digit*>.
  983.  
  984.         Occasionally, you can't use just a `/g' to get all the
  985.         changes to occur. Here are two common cases:
  986.  
  987.         # put commas in the right places in an integer
  988.         1 while s/(.*\d)(\d\d\d)/$1,$2/g;      # perl4
  989.         1 while s/(\d)(\d\d\d)(?!\d)/$1,$2/g;  # perl5
  990.  
  991.         # expand tabs to 8-column spacing
  992.         1 while s/\t+/' ' x (length($&)*8 - length($`)%8)/e;
  993.  
  994.     tr/SEARCHLIST/REPLACEMENTLIST/cds
  995.     y/SEARCHLIST/REPLACEMENTLIST/cds
  996.         Translates all occurrences of the characters found in
  997.         the search list with the corresponding character in the
  998.         replacement list. It returns the number of characters
  999.         replaced or deleted. If no string is specified via the
  1000.         =~ or !~ operator, the $_ string is translated. (The
  1001.         string specified with =~ must be a scalar variable, an
  1002.         array element, a hash element, or an assignment to one
  1003.         of those, i.e., an lvalue.) For sed devotees, `y' is
  1004.         provided as a synonym for `tr'. If the SEARCHLIST is
  1005.         delimited by bracketing quotes, the REPLACEMENTLIST has
  1006.         its own pair of quotes, which may or may not be
  1007.         bracketing quotes, e.g., `tr[A-Z][a-z]' or `tr(+-
  1008.         */)/ABCD/'.
  1009.  
  1010.         Options:
  1011.  
  1012.         c   Complement the SEARCHLIST.
  1013.         d   Delete found but unreplaced characters.
  1014.         s   Squash duplicate replaced characters.
  1015.  
  1016.         If the `/c' modifier is specified, the SEARCHLIST
  1017.         character set is complemented. If the `/d' modifier is
  1018.         specified, any characters specified by SEARCHLIST not
  1019.         found in REPLACEMENTLIST are deleted. (Note that this is
  1020.         slightly more flexible than the behavior of some tr
  1021.         programs, which delete anything they find in the
  1022.         SEARCHLIST, period.) If the `/s' modifier is specified,
  1023.         sequences of characters that were translated to the same
  1024.         character are squashed down to a single instance of the
  1025.         character.
  1026.  
  1027.         If the `/d' modifier is used, the REPLACEMENTLIST is
  1028.         always interpreted exactly as specified. Otherwise, if
  1029.         the REPLACEMENTLIST is shorter than the SEARCHLIST, the
  1030.         final character is replicated till it is long enough. If
  1031.         the REPLACEMENTLIST is null, the SEARCHLIST is
  1032.         replicated. This latter is useful for counting
  1033.         characters in a class or for squashing character
  1034.         sequences in a class.
  1035.  
  1036.         Examples:
  1037.  
  1038.         $ARGV[1] =~ tr/A-Z/a-z/;    # canonicalize to lower case
  1039.  
  1040.         $cnt = tr/*/*/;         # count the stars in $_
  1041.  
  1042.         $cnt = $sky =~ tr/*/*/;     # count the stars in $sky
  1043.  
  1044.         $cnt = tr/0-9//;        # count the digits in $_
  1045.  
  1046.         tr/a-zA-Z//s;            # bookkeeper -> bokeper
  1047.  
  1048.         ($HOST = $host) =~ tr/a-z/A-Z/;
  1049.  
  1050.         tr/a-zA-Z/ /cs;         # change non-alphas to single space
  1051.  
  1052.         tr [\200-\377]
  1053.            [\000-\177];         # delete 8th bit
  1054.  
  1055.         If multiple translations are given for a character, only
  1056.         the first one is used:
  1057.  
  1058.         tr/AAA/XYZ/
  1059.  
  1060.         will translate any A to X.
  1061.  
  1062.         Note that because the translation table is built at
  1063.         compile time, neither the SEARCHLIST nor the
  1064.         REPLACEMENTLIST are subjected to double quote
  1065.         interpolation. That means that if you want to use
  1066.         variables, you must use an eval():
  1067.  
  1068.         eval "tr/$oldlist/$newlist/";
  1069.         die $@ if $@;
  1070.  
  1071.         eval "tr/$oldlist/$newlist/, 1" or die $@;
  1072.  
  1073.   I/O Operators
  1074.  
  1075.     There are several I/O operators you should know about. A string
  1076.     is enclosed by backticks (grave accents) first undergoes
  1077.     variable substitution just like a double quoted string. It is
  1078.     then interpreted as a command, and the output of that command is
  1079.     the value of the pseudo-literal, like in a shell. In a scalar
  1080.     context, a single string consisting of all the output is
  1081.     returned. In a list context, a list of values is returned, one
  1082.     for each line of output. (You can set `$/' to use a different
  1083.     line terminator.) The command is executed each time the pseudo-
  1084.     literal is evaluated. The status value of the command is
  1085.     returned in `$?' (see the perlvar manpage for the interpretation
  1086.     of `$?'). Unlike in csh, no translation is done on the return
  1087.     data--newlines remain newlines. Unlike in any of the shells,
  1088.     single quotes do not hide variable names in the command from
  1089.     interpretation. To pass a $ through to the shell you need to
  1090.     hide it with a backslash. The generalized form of backticks is
  1091.     `qx//'. (Because backticks always undergo shell expansion as
  1092.     well, see the perlsec manpage for security concerns.)
  1093.  
  1094.     Evaluating a filehandle in angle brackets yields the next line
  1095.     from that file (newline, if any, included), or `undef' at end of
  1096.     file. Ordinarily you must assign that value to a variable, but
  1097.     there is one situation where an automatic assignment happens.
  1098.     *If and ONLY if* the input symbol is the only thing inside the
  1099.     conditional of a `while' or `for(;;)' loop, the value is
  1100.     automatically assigned to the variable `$_'. The assigned value
  1101.     is then tested to see if it is defined. (This may seem like an
  1102.     odd thing to you, but you'll use the construct in almost every
  1103.     Perl script you write.) Anyway, the following lines are
  1104.     equivalent to each other:
  1105.  
  1106.     while (defined($_ = <STDIN>)) { print; }
  1107.     while (<STDIN>) { print; }
  1108.     for (;<STDIN>;) { print; }
  1109.     print while defined($_ = <STDIN>);
  1110.     print while <STDIN>;
  1111.  
  1112.     The filehandles STDIN, STDOUT, and STDERR are predefined. (The
  1113.     filehandles `stdin', `stdout', and `stderr' will also work
  1114.     except in packages, where they would be interpreted as local
  1115.     identifiers rather than global.) Additional filehandles may be
  1116.     created with the open() function. See the "open()" entry in the
  1117.     perlfunc manpage for details on this.
  1118.  
  1119.     If a <FILEHANDLE> is used in a context that is looking for a
  1120.     list, a list consisting of all the input lines is returned, one
  1121.     line per list element. It's easy to make a *LARGE* data space
  1122.     this way, so use with care.
  1123.  
  1124.     The null filehandle <> is special and can be used to emulate the
  1125.     behavior of sed and awk. Input from <> comes either from
  1126.     standard input, or from each file listed on the command line.
  1127.     Here's how it works: the first time <> is evaluated, the @ARGV
  1128.     array is checked, and if it is null, `$ARGV[0]' is set to "-",
  1129.     which when opened gives you standard input. The @ARGV array is
  1130.     then processed as a list of filenames. The loop
  1131.  
  1132.     while (<>) {
  1133.         ...             # code for each line
  1134.     }
  1135.  
  1136.     is equivalent to the following Perl-like pseudo code:
  1137.  
  1138.     unshift(@ARGV, '-') unless @ARGV;
  1139.     while ($ARGV = shift) {
  1140.         open(ARGV, $ARGV);
  1141.         while (<ARGV>) {
  1142.         ...        # code for each line
  1143.         }
  1144.     }
  1145.  
  1146.     except that it isn't so cumbersome to say, and will actually
  1147.     work. It really does shift array @ARGV and put the current
  1148.     filename into variable $ARGV. It also uses filehandle *ARGV*
  1149.     internally--<> is just a synonym for <ARGV>, which is magical.
  1150.     (The pseudo code above doesn't work because it treats <ARGV> as
  1151.     non-magical.)
  1152.  
  1153.     You can modify @ARGV before the first <> as long as the array
  1154.     ends up containing the list of filenames you really want. Line
  1155.     numbers (`$.') continue as if the input were one big happy file.
  1156.     (But see example under eof() for how to reset line numbers on
  1157.     each file.)
  1158.  
  1159.     If you want to set @ARGV to your own list of files, go right
  1160.     ahead. If you want to pass switches into your script, you can
  1161.     use one of the Getopts modules or put a loop on the front like
  1162.     this:
  1163.  
  1164.     while ($_ = $ARGV[0], /^-/) {
  1165.         shift;
  1166.         last if /^--$/;
  1167.         if (/^-D(.*)/) { $debug = $1 }
  1168.         if (/^-v/)       { $verbose++  }
  1169.         ...         # other switches
  1170.     }
  1171.     while (<>) {
  1172.         ...         # code for each line
  1173.     }
  1174.  
  1175.     The <> symbol will return FALSE only once. If you call it again
  1176.     after this it will assume you are processing another @ARGV list,
  1177.     and if you haven't set @ARGV, will input from STDIN.
  1178.  
  1179.     If the string inside the angle brackets is a reference to a
  1180.     scalar variable (e.g., <$foo>), then that variable contains the
  1181.     name of the filehandle to input from, or a reference to the
  1182.     same. For example:
  1183.  
  1184.     $fh = \*STDIN;
  1185.     $line = <$fh>;
  1186.  
  1187.     If the string inside angle brackets is not a filehandle or a
  1188.     scalar variable containing a filehandle name or reference, then
  1189.     it is interpreted as a filename pattern to be globbed, and
  1190.     either a list of filenames or the next filename in the list is
  1191.     returned, depending on context. One level of $ interpretation is
  1192.     done first, but you can't say `<$foo>' because that's an
  1193.     indirect filehandle as explained in the previous paragraph. (In
  1194.     older versions of Perl, programmers would insert curly brackets
  1195.     to force interpretation as a filename glob: `<${foo}>'. These
  1196.     days, it's considered cleaner to call the internal function
  1197.     directly as `glob($foo)', which is probably the right way to
  1198.     have done it in the first place.) Example:
  1199.  
  1200.     while (<*.c>) {
  1201.         chmod 0644, $_;
  1202.     }
  1203.  
  1204.     is equivalent to
  1205.  
  1206.     open(FOO, "echo *.c | tr -s ' \t\r\f' '\\012\\012\\012\\012'|");
  1207.     while (<FOO>) {
  1208.         chop;
  1209.         chmod 0644, $_;
  1210.     }
  1211.  
  1212.     In fact, it's currently implemented that way. (Which means it
  1213.     will not work on filenames with spaces in them unless you have
  1214.     csh(1) on your machine.) Of course, the shortest way to do the
  1215.     above is:
  1216.  
  1217.     chmod 0644, <*.c>;
  1218.  
  1219.     Because globbing invokes a shell, it's often faster to call
  1220.     readdir() yourself and do your own grep() on the filenames.
  1221.     Furthermore, due to its current implementation of using a shell,
  1222.     the glob() routine may get "Arg list too long" errors (unless
  1223.     you've installed tcsh(1L) as /bin/csh).
  1224.  
  1225.     A glob evaluates its (embedded) argument only when it is
  1226.     starting a new list. All values must be read before it will
  1227.     start over. In a list context this isn't important, because you
  1228.     automatically get them all anyway. In a scalar context, however,
  1229.     the operator returns the next value each time it is called, or a
  1230.     FALSE value if you've just run out. Again, FALSE is returned
  1231.     only once. So if you're expecting a single value from a glob, it
  1232.     is much better to say
  1233.  
  1234.     ($file) = <blurch*>;
  1235.  
  1236.     than
  1237.  
  1238.     $file = <blurch*>;
  1239.  
  1240.     because the latter will alternate between returning a filename
  1241.     and returning FALSE.
  1242.  
  1243.     It you're trying to do variable interpolation, it's definitely
  1244.     better to use the glob() function, because the older notation
  1245.     can cause people to become confused with the indirect filehandle
  1246.     notation.
  1247.  
  1248.     @files = glob("$dir/*.[ch]");
  1249.     @files = glob($files[$i]);
  1250.  
  1251.   Constant Folding
  1252.  
  1253.     Like C, Perl does a certain amount of expression evaluation at
  1254.     compile time, whenever it determines that all of the arguments
  1255.     to an operator are static and have no side effects. In
  1256.     particular, string concatenation happens at compile time between
  1257.     literals that don't do variable substitution. Backslash
  1258.     interpretation also happens at compile time. You can say
  1259.  
  1260.     'Now is the time for all' . "\n" .
  1261.         'good men to come to.'
  1262.  
  1263.     and this all reduces to one string internally. Likewise, if you
  1264.     say
  1265.  
  1266.     foreach $file (@filenames) {
  1267.         if (-s $file > 5 + 100 * 2**16) { ... }
  1268.     }
  1269.  
  1270.     the compiler will precompute the number that expression
  1271.     represents so that the interpreter won't have to.
  1272.  
  1273.   Integer Arithmetic
  1274.  
  1275.     By default Perl assumes that it must do most of its arithmetic
  1276.     in floating point. But by saying
  1277.  
  1278.     use integer;
  1279.  
  1280.     you may tell the compiler that it's okay to use integer
  1281.     operations from here to the end of the enclosing BLOCK. An inner
  1282.     BLOCK may countermand this by saying
  1283.  
  1284.     no integer;
  1285.  
  1286.     which lasts until the end of that BLOCK.
  1287.  
  1288.     The bitwise operators ("&", "|", "^", "~", "<<", and ">>")
  1289.     always produce integral results. However, `use integer' still
  1290.     has meaning for them. By default, their results are interpreted
  1291.     as unsigned integers. However, if `use integer' is in effect,
  1292.     their results are interpreted as signed integers. For example,
  1293.     `~0' usually evaluates to a large integral value. However, `use
  1294.     integer; ~0' is -1.
  1295.  
  1296.   Floating-point Arithmetic
  1297.  
  1298.     While `use integer' provides integer-only arithmetic, there is
  1299.     no similar ways to provide rounding or truncation at a certain
  1300.     number of decimal places. For rounding to a certain number of
  1301.     digits, sprintf() or printf() is usually the easiest route.
  1302.  
  1303.     The POSIX module (part of the standard perl distribution)
  1304.     implements ceil(), floor(), and a number of other mathematical
  1305.     and trigonometric functions. The Math::Complex module (part of
  1306.     the standard perl distribution) defines a number of mathematical
  1307.     functions that can also work on real numbers. Math::Complex not
  1308.     as efficient as POSIX, but POSIX can't work with complex
  1309.     numbers.
  1310.  
  1311.     Rounding in financial applications can have serious
  1312.     implications, and the rounding method used should be specified
  1313.     precisely. In these cases, it probably pays not to trust
  1314.     whichever system rounding is being used by Perl, but to instead
  1315.     implement the rounding function you need yourself.
  1316.  
  1317.