print $i++; # prints 0
print ++$j; # prints 1
+Note that just as in C, Perl doesn't define B<when> the variable is
+incremented or decremented. You just know it will be done sometime
+before or after the value is returned. This also means that modifying
+a variable twice in the same statement will lead to undefined behaviour.
+Avoid statements like:
+
+ $i = $i ++;
+ print ++ $i + $i ++;
+
+Perl will not guarantee what the result of the above statements is.
+
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
the operand is an identifier, a string consisting of a minus sign
concatenated with the identifier is returned. Otherwise, if the string
starts with a plus or minus, a string starting with the opposite sign
-is returned. One effect of these rules is that C<-bareword> is equivalent
-to C<"-bareword">.
+is returned. One effect of these rules is that -bareword is equivalent
+to the string "-bareword".
Unary "~" performs bitwise negation, i.e., 1's complement. For
example, C<0666 & ~027> is 0640. (See also L<Integer Arithmetic> and
supposed to be searched, substituted, or transliterated instead of the default
$_. When used in scalar context, the return value generally indicates the
success of the operation. Behavior in list context depends on the particular
-operator. See L</"Regexp Quote-Like Operators"> for details.
+operator. See L</"Regexp Quote-Like Operators"> for details and
+L<perlretut> for examples using these operators.
If the right argument is an expression rather than a search pattern,
substitution, or transliteration, it is interpreted as a search pattern at run
operand is not enclosed in parentheses, it returns a string consisting
of the left operand repeated the number of times specified by the right
operand. In list context, if the left operand is enclosed in
-parentheses, it repeats the list.
+parentheses or is a list formed by C<qw/STRING/>, it repeats the list.
+If the right operand is zero or negative, it returns an empty string
+or an empty list, depending on the context.
print '-' x 80; # print row of dashes
close ARGV if eof; # reset $. each file
}
-As a list operator:
+Here's a simple example to illustrate the difference between
+the two range operators:
+
+ @lines = (" - Foo",
+ "01 - Bar",
+ "1 - Baz",
+ " - Quux");
+
+ foreach(@lines)
+ {
+ if (/0/ .. /1/)
+ {
+ print "$_\n";
+ }
+ }
+
+This program will print only the line containing "Bar". If
+the range operator is changed to C<...>, it will also print the
+"Baz" line.
+
+And now some examples as a list operator:
for (101 .. 200) { print; } # print $_ 100 times
@foo = @foo[0 .. $#foo]; # an expensive no-op
both its arguments into the list.
The C<< => >> operator is a synonym for the comma, but forces any word
-to its left to be interpreted as a string (as of 5.001). It is helpful
-in documenting the correspondence between keys and values in hashes,
-and other paired elements in lists.
+(consisting entirely of word characters) to its left to be interpreted
+as a string (as of 5.001). This includes words that might otherwise be
+considered a constant or function call.
+
+ use constant FOO => "something";
+
+ my %h = ( FOO => 23 );
+
+is equivalent to:
+
+ my %h = ("FOO", 23);
+
+It is I<NOT>:
+
+ my %h = ("something", 23);
+
+If the argument on the left is not a word, it is first interpreted as
+an expression, and then the string value of that is used.
+
+The C<< => >> operator is helpful in documenting the correspondence
+between keys and values in hashes, and other paired elements in lists.
+
+ %hash = ( $key => $value );
+ login( $username => $password );
=head2 List Operators (Rightward)
\c[ control char (ESC)
\N{name} named Unicode character
+B<NOTE>: Unlike C and other languages, Perl has no \v escape sequence for
+the vertical tab (VT - ASCII 11).
+
The following escape sequences are available in constructs that interpolate
but not in transliterations.
$output = `cmd 3>&1 1>&2 2>&3 3>&-`;
To read both a command's STDOUT and its STDERR separately, it's easiest
-and safest to redirect them separately to files, and then read from those
-files when the program is done:
+to redirect them separately to files, and then read from those files
+when the program is done:
- system("program args 1>/tmp/program.stdout 2>/tmp/program.stderr");
+ system("program args 1>program.stdout 2>program.stderr");
Using single-quote as a delimiter protects the command from Perl's
double-quote interpolation, passing it on to the shell instead:
\*/ # Match the closing delimiter.
} []gsx;
- s/^\s*(.*?)\s*$/$1/; # trim white space in $_, expensively
+ s/^\s*(.*?)\s*$/$1/; # trim whitespace in $_, expensively
- for ($variable) { # trim white space in $variable, cheap
+ for ($variable) { # trim whitespace in $variable, cheap
s/^\s+//;
s/\s+$//;
}
the example above is not C<m//x>, but rather C<m//> with no C</x>
modifier. So the embedded C<#> is interpreted as a literal C<#>.
+Also no attention is paid to C<\c\> during this search.
+Thus the second C<\> in C<qq/\c\/> is interpreted as a part of C<\/>,
+and the following C</> is not recognized as a delimiter.
+Instead, use C<\034> or C<\x1c> at the end of quoted constructs.
+
=item Removal of backslashes before delimiters
During the second pass, text between the starting and ending
grounds alone. That means C<< <$x> >> is always a readline() from
an indirect handle, but C<< <$hash{key}> >> is always a glob().
That's because $x is a simple scalar variable, but C<$hash{key}> is
-not--it's a hash element.
+not--it's a hash element. Even C<< <$x > >> (note the extra space)
+is treated as C<glob("$x ")>, not C<readline($x)>.
One level of double-quote interpretation is done first, but you can't
say C<< <$foo> >> because that's an indirect filehandle as explained
the compiler will precompute the number which that expression
represents so that the interpreter won't have to.
+=head2 No-ops
+
+Perl doesn't officially have a no-op operator, but the bare constants
+C<0> and C<1> are special-cased to not produce a warning in a void
+context, so you can for example safely do
+
+ 1 while foo();
+
=head2 Bitwise String Operators
Bitstrings of any size may be manipulated by the bitwise operators