Some more perlio doc tweaks based on NI-S.
[p5sagit/p5-mst-13.2.git] / pod / perlop.pod
index 8af2d5b..1550660 100644 (file)
@@ -127,6 +127,10 @@ character within its range, with carry:
     print ++($foo = 'Az');     # prints 'Ba'
     print ++($foo = 'zz');     # prints 'aaa'
 
+C<undef> is always treated as numeric, and in particular is changed
+to C<0> before incrementing (so that a post-increment of an undef value
+will return C<0> rather than C<undef>).
+
 The auto-decrement operator is not magical.
 
 =head2 Exponentiation
@@ -178,8 +182,7 @@ operator.  See L</"Regexp Quote-Like Operators"> for details.
 
 If the right argument is an expression rather than a search pattern,
 substitution, or transliteration, it is interpreted as a search pattern at run
-time.  This can be less efficient than an explicit search, because the
-pattern must be compiled every time the expression is evaluated.
+time.
 
 Binary "!~" is just like "=~" except the return value is negated in
 the logical sense.
@@ -196,7 +199,7 @@ C<$a> minus the largest multiple of C<$b> that is not greater than
 C<$a>.  If C<$b> is negative, then C<$a % $b> is C<$a> minus the
 smallest multiple of C<$b> that is not less than C<$a> (i.e. the
 result will be less than or equal to zero). 
-Note than when C<use integer> is in scope, "%" gives you direct access
+Note that when C<use integer> is in scope, "%" gives you direct access
 to the modulus operator as implemented by your C compiler.  This
 operator is not as well defined for negative operands, but it will
 execute faster.
@@ -336,17 +339,27 @@ by the current locale if C<use locale> is in effect.  See L<perllocale>.
 
 =head2 Bitwise And
 
-Binary "&" returns its operators ANDed together bit by bit.
+Binary "&" returns its operands ANDed together bit by bit.
 (See also L<Integer Arithmetic> and L<Bitwise String Operators>.)
 
+Note that "&" has lower priority than relational operators, so for example
+the brackets are essential in a test like
+
+       print "Even\n" if ($x & 1) == 0;
+
 =head2 Bitwise Or and Exclusive Or
 
-Binary "|" returns its operators ORed together bit by bit.
+Binary "|" returns its operands ORed together bit by bit.
 (See also L<Integer Arithmetic> and L<Bitwise String Operators>.)
 
-Binary "^" returns its operators XORed together bit by bit.
+Binary "^" returns its operands XORed together bit by bit.
 (See also L<Integer Arithmetic> and L<Bitwise String Operators>.)
 
+Note that "|" and "^" have lower priority than relational operators, so
+for example the brackets are essential in a test like
+
+       print "false\n" if (8 | 2) != 10;
+
 =head2 C-style Logical And
 
 Binary "&&" performs a short-circuit logical AND operation.  That is,
@@ -405,10 +418,10 @@ Using "or" for assignment is unlikely to do what you want; see below.
 =head2 Range Operators
 
 Binary ".." is the range operator, which is really two different
-operators depending on the context.  In list context, it returns an
+operators depending on the context.  In list context, it returns a
 list of values counting (up by ones) from the left value to the right
 value.  If the left value is greater than the right value then it
-returns the empty array.  The range operator is useful for writing
+returns the empty list.  The range operator is useful for writing
 C<foreach (1..10)> loops and for doing slice operations on arrays. In
 the current implementation, no temporary array is created when the
 range operator is used as the expression in C<foreach> loops, but older
@@ -445,23 +458,42 @@ sequence number in a range has the string "E0" appended to it, which
 doesn't affect its numeric value, but gives you something to search
 for if you want to exclude the endpoint.  You can exclude the
 beginning point by waiting for the sequence number to be greater
-than 1.  If either operand of scalar ".." is a constant expression,
-that operand is implicitly compared to the C<$.> variable, the
-current line number.  Examples:
+than 1.
+
+If either operand of scalar ".." is a constant expression,
+that operand is considered true if it is equal (C<==>) to the current
+input line number (the C<$.> variable).
+
+To be pedantic, the comparison is actually C<int(EXPR) == int(EXPR)>,
+but that is only an issue if you use a floating point expression; when
+implicitly using C<$.> as described in the previous paragraph, the
+comparison is C<int(EXPR) == int($.)> which is only an issue when C<$.>
+is set to a floating point value and you are not reading from a file.
+Furthermore, C<"span" .. "spat"> or C<2.18 .. 3.14> will not do what
+you want in scalar context because each of the operands are evaluated
+using their integer representation.
+
+Examples:
 
 As a scalar operator:
 
-    if (101 .. 200) { print; } # print 2nd hundred lines
-    next line if (1 .. /^$/);  # skip header lines
+    if (101 .. 200) { print; } # print 2nd hundred lines, short for
+                               #   if ($. == 101 .. $. == 200) ...
+    next line if (1 .. /^$/);  # skip header lines, short for
+                               #   ... if ($. == 1 .. /^$/);
     s/^/> / if (/^$/ .. eof());        # quote body
 
     # parse mail messages
     while (<>) {
         $in_header =   1  .. /^$/;
-        $in_body   = /^$/ .. eof();
-       # do something based on those
+        $in_body   = /^$/ .. eof;
+        if ($in_header) {
+            # ...
+        } else { # in body
+            # ...
+        }
     } continue {
-       close ARGV if eof;              # reset $. each file
+        close ARGV if eof;             # reset $. each file
     }
 
 As a list operator:
@@ -489,6 +521,11 @@ in the sequence that the magical increment would produce, the sequence
 goes until the next value would be longer than the final value
 specified.
 
+Because each operand is evaluated in integer form, C<2.18 .. 3.14> will
+return two elements in list context.
+
+    @list = (2.18 .. 3.14); # same as @list = (2 .. 3);
+
 =head2 Conditional Operator
 
 Ternary "?:" is the conditional operator, just as in C.  It works much
@@ -763,8 +800,8 @@ you may be burned some day.
 
 For constructs that do interpolate, variables beginning with "C<$>"
 or "C<@>" are interpolated.  Subscripted variables such as C<$a[3]> or
-C<$href->{key}[0]> are also interpolated, as are array and hash slices.
-But method calls such as C<$obj->meth> are not.
+C<< $href->{key}[0] >> are also interpolated, as are array and hash slices.
+But method calls such as C<< $obj->meth >> are not.
 
 Interpolating an array or slice interpolates the elements in order,
 separated by the value of C<$">, so is equivalent to interpolating
@@ -1165,7 +1202,8 @@ equivalent to:
 
     split(' ', q/STRING/);
 
-the difference being that it generates a real list at compile time.  So
+the differences being that it generates a real list at compile time, and
+in scalar context it returns the last element in the list.  So
 this expression:
 
     qw(foo bar baz)
@@ -1653,7 +1691,7 @@ alphanumeric char, as in:
 
 In the RE above, which is intentionally obfuscated for illustration, the
 delimiter is C<m>, the modifier is C<mx>, and after backslash-removal the
-RE is the same as for C<m/ ^ a s* b /mx>).  There's more than one 
+RE is the same as for C<m/ ^ a \s* b /mx>.  There's more than one 
 reason you're encouraged to restrict your delimiters to non-alphanumeric,
 non-whitespace choices.