Front page | perl.perl5.porters |
Postings from October 2003
[PATCH] perlop.pod Revamp
Thread Next
From:
Shlomi Fish
Date:
October 4, 2003 09:56
Subject:
[PATCH] perlop.pod Revamp
Message ID:
Pine.LNX.4.56.0310041854130.26791@vipe.technion.ac.il
Index: pod/perlop.pod
===================================================================
--- pod/perlop.pod (revision 6)
+++ pod/perlop.pod (working copy)
@@ -4,6 +4,13 @@
=head1 SYNOPSIS
+Precendence in operators means that some operators are evaluated before
+others. For example, in the mathematical expression "2+4*5", the
+multiplication has higher precendence because 4*5 is evaluated first
+(yielding 2+20 = 22) and not 6*5 == 30. Associativity means what happens
+if a sequence of the same operators is used one after another: whether
+the evaluator will evaluate the left operations first or the right.
+
Perl operators have the following associativity and precedence,
listed from highest precedence to lowest. Operators borrowed from
C keep the same precedence relationship with each other, even where
@@ -83,9 +90,15 @@
print ($foo & 255) + 1, "\n";
-probably doesn't do what you expect at first glance. See
-L<Named Unary Operators> for more discussion of this.
+probably doesn't do what you expect at first glance. What happens
+is that the parenthesis enclose the entire arguments list to print (and
+later 1 is added to its return value and "\n" is list-concatenated) To do this
+properly one has to write:
+ print (($foo & 255) + 1, "\n");
+
+See L<Named Unary Operators> for more discussion of this.
+
Also parsed as terms are the C<do {}> and C<eval {}> constructs, as
well as subroutine and method calls, and the anonymous
constructors C<[]> and C<{}>.
@@ -110,16 +123,20 @@
=head2 Auto-increment and Auto-decrement
-"++" and "--" work as in C. That is, if placed before a variable, they
-increment or decrement the variable before returning the value, and if
-placed after, increment or decrement the variable after returning the value.
+"++" and "--" work as in C. That is, if placed before or after a
+variable, they increase or decrease the value of the variable by
+one. If placed before the variable, they return the value after the
+modification, and if placed afterwards, they return the value before
+the modification.
The auto-increment operator has a little extra builtin magic to it. If
you increment a variable that is numeric, or that has ever been used in
a numeric context, you get a normal increment. If, however, the
variable has been used in only string contexts since it was set, and
has a value that is not the empty string and matches the pattern
-C</^[a-zA-Z]*[0-9]*\z/>, the increment is done as a string, preserving each
+C</^[a-zA-Z]*[0-9]*\z/> (which means any number of characters,
+including zero, followed by any number of digits followed by the end
+of the string), the increment is done as a string, preserving each
character within its range, with carry:
print ++($foo = '99'); # prints '100'
@@ -157,7 +174,7 @@
L<Bitwise String Operators>.) Note that the width of the result is
platform-dependent: ~0 is 32 bits wide on a 32-bit platform, but 64
bits wide on a 64-bit platform, so if you are expecting a certain bit
-width, remember use the & operator to mask off the excess bits.
+width, remember to use the & operator to mask off the excess bits.
Unary "+" has no effect whatsoever, even on strings. It is useful
syntactically for separating a function name from a parenthesized expression
@@ -224,7 +241,8 @@
Binary "-" returns the difference of two numbers.
-Binary "." concatenates two strings.
+Binary "." concatenates two strings. This means that C<$a.$b> will give
+a string that contains the string C<$a> followed by the string C<$b>.
=head2 Shift Operators
@@ -385,9 +403,9 @@
for variables. If you actually want to test if at least one of C<$a> and C<$b> is
defined, use C<defined($a // $b)>.
-The C<||>, C<//> and C<&&> operators differ from C's in that, rather than returning
-0 or 1, they return the last value evaluated. Thus, a reasonably portable
-way to find out the home directory might be:
+The C<||>, C<//> and C<&&> operators differ from C's in that, rather than
+returning 0 or 1, they return the last value evaluated. Thus, a reasonably
+portable way to find out the home directory might be:
$home = $ENV{'HOME'} // $ENV{'LOGDIR'} //
(getpwuid($<))[7] // die "You're homeless!\n";
@@ -619,9 +637,10 @@
In list context, it's just the list argument separator, and inserts
both its arguments into the list.
-The => digraph is mostly just a synonym for the comma operator. It's useful for
-documenting arguments that come in pairs. As of release 5.001, it also forces
-any word to the left of it to be interpreted as a string.
+The C<=E<gt>> digraph is mostly just a synonym for the comma operator.
+It's useful for documenting arguments that come in pairs. As of
+release 5.001, it also forces any word to the left of it to be
+interpreted as a string.
=head2 List Operators (Rightward)
Thread Next
-
[PATCH] perlop.pod Revamp
by Shlomi Fish