12345
12345.67
.23E-10 # a very small number
+ 3.14_15_92 # a very important number
4_294_967_296 # underscore for legibility
0xff # hex
+ 0xdead_beef # more hex
0377 # octal
0b011011 # binary
-You are allowed to use an underscore in numeric literals for legibility,
-but in decimal numeric literals (those written in base 10, not
-necessarily with a fractional part), digits may only be grouped in
-threes. For decimal numeric literals containing a fractional part,
-this applies only to the part before the decimal point; the fractional
-part (but not the exponent, if given!) may contain underscores
-anywhere you feel it enhances legibility. Binary, octal, and
-hexadecimal numeric literals may contain underscores in any place --
-so you could, for example, group binary digits by threes (as for a
-Unix-style mode argument such as 0b110_100_100) or by fours (to
-represent nibbles, as in 0b1010_0110) or in other groups.
+You are allowed to use underscores (underbars) in numeric literals
+between digits for legibility. You could, for example, group binary
+digits by threes (as for a Unix-style mode argument such as 0b110_100_100)
+or by fours (to represent nibbles, as in 0b1010_0110) or in other groups.
String literals are usually delimited by either single or double
quotes. They work much like quotes in the standard Unix shells:
array had been interpolated at that point.
This interpolation combines with the facts that the opening
-and closing parentheses are optional (except necessary for
+and closing parentheses are optional (except when necessary for
precedence) and lists may end with an optional comma to mean that
multiple commas within lists are legal syntax. The list C<1,,3> is a
concatenation of two lists, C<1,> and C<3>, the first of which ends
context, because most list functions return a null list when finished,
which when assigned produces a 0, which is interpreted as FALSE.
-The final element may be an array or a hash:
+It's also the source of a useful idiom for executing a function or
+performing an operation in list context and then counting the number of
+return values, by assigning to an empty list and then using that
+assignment in scalar context. For example, this code:
+
+ $count = () = $string =~ /\d+/g;
+
+will place into $count the number of digit groups found in $string.
+This happens because the pattern match is in list context (since it
+is being assigned to the empty list), and will therefore return a list
+of all matching parts of the string. The list assignment in scalar
+context will translate that into the number of elements (here, the
+number of times the pattern matched) and assign that to $count. Note
+that simply using
+
+ $count = $string =~ /\d+/g;
+
+would not have worked, since a pattern match in scalar context will
+only return true or false, rather than a count of matches.
+
+The final element of a list assignment may be an array or a hash:
($a, $b, @rest) = split;
my($a, $b, %rest) = @_;