1PERLOP(1) Perl Programmers Reference Guide PERLOP(1)
2
3
4
6 perlop - Perl operators and precedence
7
9 In Perl, the operator determines what operation is performed,
10 independent of the type of the operands. For example "$x + $y" is
11 always a numeric addition, and if $x or $y do not contain numbers, an
12 attempt is made to convert them to numbers first.
13
14 This is in contrast to many other dynamic languages, where the
15 operation is determined by the type of the first argument. It also
16 means that Perl has two versions of some operators, one for numeric and
17 one for string comparison. For example "$x == $y" compares two numbers
18 for equality, and "$x eq $y" compares two strings.
19
20 There are a few exceptions though: "x" can be either string repetition
21 or list repetition, depending on the type of the left operand, and "&",
22 "|", "^" and "~" can be either string or numeric bit operations.
23
24 Operator Precedence and Associativity
25 Operator precedence and associativity work in Perl more or less like
26 they do in mathematics.
27
28 Operator precedence means some operators group more tightly than
29 others. For example, in "2 + 4 * 5", the multiplication has higher
30 precedence, so "4 * 5" is grouped together as the right-hand operand of
31 the addition, rather than "2 + 4" being grouped together as the left-
32 hand operand of the multiplication. It is as if the expression were
33 written "2 + (4 * 5)", not "(2 + 4) * 5". So the expression yields "2 +
34 20 == 22", rather than "6 * 5 == 30".
35
36 Operator associativity defines what happens if a sequence of the same
37 operators is used one after another: usually that they will be grouped
38 at the left or the right. For example, in "9 - 3 - 2", subtraction is
39 left associative, so "9 - 3" is grouped together as the left-hand
40 operand of the second subtraction, rather than "3 - 2" being grouped
41 together as the right-hand operand of the first subtraction. It is as
42 if the expression were written "(9 - 3) - 2", not "9 - (3 - 2)". So the
43 expression yields "6 - 2 == 4", rather than "9 - 1 == 8".
44
45 For simple operators that evaluate all their operands and then combine
46 the values in some way, precedence and associativity (and parentheses)
47 imply some ordering requirements on those combining operations. For
48 example, in "2 + 4 * 5", the grouping implied by precedence means that
49 the multiplication of 4 and 5 must be performed before the addition of
50 2 and 20, simply because the result of that multiplication is required
51 as one of the operands of the addition. But the order of operations is
52 not fully determined by this: in "2 * 2 + 4 * 5" both multiplications
53 must be performed before the addition, but the grouping does not say
54 anything about the order in which the two multiplications are
55 performed. In fact Perl has a general rule that the operands of an
56 operator are evaluated in left-to-right order. A few operators such as
57 "&&=" have special evaluation rules that can result in an operand not
58 being evaluated at all; in general, the top-level operator in an
59 expression has control of operand evaluation.
60
61 Some comparison operators, as their associativity, chain with some
62 operators of the same precedence (but never with operators of different
63 precedence). This chaining means that each comparison is performed on
64 the two arguments surrounding it, with each interior argument taking
65 part in two comparisons, and the comparison results are implicitly
66 ANDed. Thus "$x < $y <= $z" behaves exactly like
67 "$x < $y && $y <= $z", assuming that "$y" is as simple a scalar as it
68 looks. The ANDing short-circuits just like "&&" does, stopping the
69 sequence of comparisons as soon as one yields false.
70
71 In a chained comparison, each argument expression is evaluated at most
72 once, even if it takes part in two comparisons, but the result of the
73 evaluation is fetched for each comparison. (It is not evaluated at all
74 if the short-circuiting means that it's not required for any
75 comparisons.) This matters if the computation of an interior argument
76 is expensive or non-deterministic. For example,
77
78 if($x < expensive_sub() <= $z) { ...
79
80 is not entirely like
81
82 if($x < expensive_sub() && expensive_sub() <= $z) { ...
83
84 but instead closer to
85
86 my $tmp = expensive_sub();
87 if($x < $tmp && $tmp <= $z) { ...
88
89 in that the subroutine is only called once. However, it's not exactly
90 like this latter code either, because the chained comparison doesn't
91 actually involve any temporary variable (named or otherwise): there is
92 no assignment. This doesn't make much difference where the expression
93 is a call to an ordinary subroutine, but matters more with an lvalue
94 subroutine, or if the argument expression yields some unusual kind of
95 scalar by other means. For example, if the argument expression yields
96 a tied scalar, then the expression is evaluated to produce that scalar
97 at most once, but the value of that scalar may be fetched up to twice,
98 once for each comparison in which it is actually used.
99
100 In this example, the expression is evaluated only once, and the tied
101 scalar (the result of the expression) is fetched for each comparison
102 that uses it.
103
104 if ($x < $tied_scalar < $z) { ...
105
106 In the next example, the expression is evaluated only once, and the
107 tied scalar is fetched once as part of the operation within the
108 expression. The result of that operation is fetched for each
109 comparison, which normally doesn't matter unless that expression result
110 is also magical due to operator overloading.
111
112 if ($x < $tied_scalar + 42 < $z) { ...
113
114 Some operators are instead non-associative, meaning that it is a syntax
115 error to use a sequence of those operators of the same precedence. For
116 example, "$x .. $y .. $z" is an error.
117
118 Perl operators have the following associativity and precedence, listed
119 from highest precedence to lowest. Operators borrowed from C keep the
120 same precedence relationship with each other, even where C's precedence
121 is slightly screwy. (This makes learning Perl easier for C folks.)
122 With very few exceptions, these all operate on scalar values only, not
123 array values.
124
125 left terms and list operators (leftward)
126 left ->
127 nonassoc ++ --
128 right **
129 right ! ~ ~. \ and unary + and -
130 left =~ !~
131 left * / % x
132 left + - .
133 left << >>
134 nonassoc named unary operators
135 nonassoc isa
136 chained < > <= >= lt gt le ge
137 chain/na == != eq ne <=> cmp ~~
138 left & &.
139 left | |. ^ ^.
140 left &&
141 left || //
142 nonassoc .. ...
143 right ?:
144 right = += -= *= etc. goto last next redo dump
145 left , =>
146 nonassoc list operators (rightward)
147 right not
148 left and
149 left or xor
150
151 In the following sections, these operators are covered in detail, in
152 the same order in which they appear in the table above.
153
154 Many operators can be overloaded for objects. See overload.
155
156 Terms and List Operators (Leftward)
157 A TERM has the highest precedence in Perl. They include variables,
158 quote and quote-like operators, any expression in parentheses, and any
159 function whose arguments are parenthesized. Actually, there aren't
160 really functions in this sense, just list operators and unary operators
161 behaving as functions because you put parentheses around the arguments.
162 These are all documented in perlfunc.
163
164 If any list operator ("print()", etc.) or any unary operator
165 ("chdir()", etc.) is followed by a left parenthesis as the next token,
166 the operator and arguments within parentheses are taken to be of
167 highest precedence, just like a normal function call.
168
169 In the absence of parentheses, the precedence of list operators such as
170 "print", "sort", or "chmod" is either very high or very low depending
171 on whether you are looking at the left side or the right side of the
172 operator. For example, in
173
174 @ary = (1, 3, sort 4, 2);
175 print @ary; # prints 1324
176
177 the commas on the right of the "sort" are evaluated before the "sort",
178 but the commas on the left are evaluated after. In other words, list
179 operators tend to gobble up all arguments that follow, and then act
180 like a simple TERM with regard to the preceding expression. Be careful
181 with parentheses:
182
183 # These evaluate exit before doing the print:
184 print($foo, exit); # Obviously not what you want.
185 print $foo, exit; # Nor is this.
186
187 # These do the print before evaluating exit:
188 (print $foo), exit; # This is what you want.
189 print($foo), exit; # Or this.
190 print ($foo), exit; # Or even this.
191
192 Also note that
193
194 print ($foo & 255) + 1, "\n";
195
196 probably doesn't do what you expect at first glance. The parentheses
197 enclose the argument list for "print" which is evaluated (printing the
198 result of "$foo & 255"). Then one is added to the return value of
199 "print" (usually 1). The result is something like this:
200
201 1 + 1, "\n"; # Obviously not what you meant.
202
203 To do what you meant properly, you must write:
204
205 print(($foo & 255) + 1, "\n");
206
207 See "Named Unary Operators" for more discussion of this.
208
209 Also parsed as terms are the "do {}" and "eval {}" constructs, as well
210 as subroutine and method calls, and the anonymous constructors "[]" and
211 "{}".
212
213 See also "Quote and Quote-like Operators" toward the end of this
214 section, as well as "I/O Operators".
215
216 The Arrow Operator
217 ""->"" is an infix dereference operator, just as it is in C and C++.
218 If the right side is either a "[...]", "{...}", or a "(...)" subscript,
219 then the left side must be either a hard or symbolic reference to an
220 array, a hash, or a subroutine respectively. (Or technically speaking,
221 a location capable of holding a hard reference, if it's an array or
222 hash reference being used for assignment.) See perlreftut and perlref.
223
224 Otherwise, the right side is a method name or a simple scalar variable
225 containing either the method name or a subroutine reference, and the
226 left side must be either an object (a blessed reference) or a class
227 name (that is, a package name). See perlobj.
228
229 The dereferencing cases (as opposed to method-calling cases) are
230 somewhat extended by the "postderef" feature. For the details of that
231 feature, consult "Postfix Dereference Syntax" in perlref.
232
233 Auto-increment and Auto-decrement
234 "++" and "--" work as in C. That is, if placed before a variable, they
235 increment or decrement the variable by one before returning the value,
236 and if placed after, increment or decrement after returning the value.
237
238 $i = 0; $j = 0;
239 print $i++; # prints 0
240 print ++$j; # prints 1
241
242 Note that just as in C, Perl doesn't define when the variable is
243 incremented or decremented. You just know it will be done sometime
244 before or after the value is returned. This also means that modifying
245 a variable twice in the same statement will lead to undefined behavior.
246 Avoid statements like:
247
248 $i = $i ++;
249 print ++ $i + $i ++;
250
251 Perl will not guarantee what the result of the above statements is.
252
253 The auto-increment operator has a little extra builtin magic to it. If
254 you increment a variable that is numeric, or that has ever been used in
255 a numeric context, you get a normal increment. If, however, the
256 variable has been used in only string contexts since it was set, and
257 has a value that is not the empty string and matches the pattern
258 "/^[a-zA-Z]*[0-9]*\z/", the increment is done as a string, preserving
259 each character within its range, with carry:
260
261 print ++($foo = "99"); # prints "100"
262 print ++($foo = "a0"); # prints "a1"
263 print ++($foo = "Az"); # prints "Ba"
264 print ++($foo = "zz"); # prints "aaa"
265
266 "undef" is always treated as numeric, and in particular is changed to 0
267 before incrementing (so that a post-increment of an undef value will
268 return 0 rather than "undef").
269
270 The auto-decrement operator is not magical.
271
272 Exponentiation
273 Binary "**" is the exponentiation operator. It binds even more tightly
274 than unary minus, so "-2**4" is "-(2**4)", not "(-2)**4". (This is
275 implemented using C's pow(3) function, which actually works on doubles
276 internally.)
277
278 Note that certain exponentiation expressions are ill-defined: these
279 include "0**0", "1**Inf", and "Inf**0". Do not expect any particular
280 results from these special cases, the results are platform-dependent.
281
282 Symbolic Unary Operators
283 Unary "!" performs logical negation, that is, "not". See also "not"
284 for a lower precedence version of this.
285
286 Unary "-" performs arithmetic negation if the operand is numeric,
287 including any string that looks like a number. If the operand is an
288 identifier, a string consisting of a minus sign concatenated with the
289 identifier is returned. Otherwise, if the string starts with a plus or
290 minus, a string starting with the opposite sign is returned. One
291 effect of these rules is that "-bareword" is equivalent to the string
292 "-bareword". If, however, the string begins with a non-alphabetic
293 character (excluding "+" or "-"), Perl will attempt to convert the
294 string to a numeric, and the arithmetic negation is performed. If the
295 string cannot be cleanly converted to a numeric, Perl will give the
296 warning Argument "the string" isn't numeric in negation (-) at ....
297
298 Unary "~" performs bitwise negation, that is, 1's complement. For
299 example, "0666 & ~027" is 0640. (See also "Integer Arithmetic" and
300 "Bitwise String Operators".) Note that the width of the result is
301 platform-dependent: "~0" is 32 bits wide on a 32-bit platform, but 64
302 bits wide on a 64-bit platform, so if you are expecting a certain bit
303 width, remember to use the "&" operator to mask off the excess bits.
304
305 Starting in Perl 5.28, it is a fatal error to try to complement a
306 string containing a character with an ordinal value above 255.
307
308 If the "bitwise" feature is enabled via "use feature 'bitwise'" or "use
309 v5.28", then unary "~" always treats its argument as a number, and an
310 alternate form of the operator, "~.", always treats its argument as a
311 string. So "~0" and "~"0"" will both give 2**32-1 on 32-bit platforms,
312 whereas "~.0" and "~."0"" will both yield "\xff". Until Perl 5.28,
313 this feature produced a warning in the "experimental::bitwise"
314 category.
315
316 Unary "+" has no effect whatsoever, even on strings. It is useful
317 syntactically for separating a function name from a parenthesized
318 expression that would otherwise be interpreted as the complete list of
319 function arguments. (See examples above under "Terms and List
320 Operators (Leftward)".)
321
322 Unary "\" creates references. If its operand is a single sigilled
323 thing, it creates a reference to that object. If its operand is a
324 parenthesised list, then it creates references to the things mentioned
325 in the list. Otherwise it puts its operand in list context, and
326 creates a list of references to the scalars in the list provided by the
327 operand. See perlreftut and perlref. Do not confuse this behavior
328 with the behavior of backslash within a string, although both forms do
329 convey the notion of protecting the next thing from interpolation.
330
331 Binding Operators
332 Binary "=~" binds a scalar expression to a pattern match. Certain
333 operations search or modify the string $_ by default. This operator
334 makes that kind of operation work on some other string. The right
335 argument is a search pattern, substitution, or transliteration. The
336 left argument is what is supposed to be searched, substituted, or
337 transliterated instead of the default $_. When used in scalar context,
338 the return value generally indicates the success of the operation. The
339 exceptions are substitution ("s///") and transliteration ("y///") with
340 the "/r" (non-destructive) option, which cause the return value to be
341 the result of the substitution. Behavior in list context depends on
342 the particular operator. See "Regexp Quote-Like Operators" for details
343 and perlretut for examples using these operators.
344
345 If the right argument is an expression rather than a search pattern,
346 substitution, or transliteration, it is interpreted as a search pattern
347 at run time. Note that this means that its contents will be
348 interpolated twice, so
349
350 '\\' =~ q'\\';
351
352 is not ok, as the regex engine will end up trying to compile the
353 pattern "\", which it will consider a syntax error.
354
355 Binary "!~" is just like "=~" except the return value is negated in the
356 logical sense.
357
358 Binary "!~" with a non-destructive substitution ("s///r") or
359 transliteration ("y///r") is a syntax error.
360
361 Multiplicative Operators
362 Binary "*" multiplies two numbers.
363
364 Binary "/" divides two numbers.
365
366 Binary "%" is the modulo operator, which computes the division
367 remainder of its first argument with respect to its second argument.
368 Given integer operands $m and $n: If $n is positive, then "$m % $n" is
369 $m minus the largest multiple of $n less than or equal to $m. If $n is
370 negative, then "$m % $n" is $m minus the smallest multiple of $n that
371 is not less than $m (that is, the result will be less than or equal to
372 zero). If the operands $m and $n are floating point values and the
373 absolute value of $n (that is "abs($n)") is less than "(UV_MAX + 1)",
374 only the integer portion of $m and $n will be used in the operation
375 (Note: here "UV_MAX" means the maximum of the unsigned integer type).
376 If the absolute value of the right operand ("abs($n)") is greater than
377 or equal to "(UV_MAX + 1)", "%" computes the floating-point remainder
378 $r in the equation "($r = $m - $i*$n)" where $i is a certain integer
379 that makes $r have the same sign as the right operand $n (not as the
380 left operand $m like C function "fmod()") and the absolute value less
381 than that of $n. Note that when "use integer" is in scope, "%" gives
382 you direct access to the modulo operator as implemented by your C
383 compiler. This operator is not as well defined for negative operands,
384 but it will execute faster.
385
386 Binary "x" is the repetition operator. In scalar context, or if the
387 left operand is neither enclosed in parentheses nor a "qw//" list, it
388 performs a string repetition. In that case it supplies scalar context
389 to the left operand, and returns a string consisting of the left
390 operand string repeated the number of times specified by the right
391 operand. If the "x" is in list context, and the left operand is either
392 enclosed in parentheses or a "qw//" list, it performs a list
393 repetition. In that case it supplies list context to the left operand,
394 and returns a list consisting of the left operand list repeated the
395 number of times specified by the right operand. If the right operand
396 is zero or negative (raising a warning on negative), it returns an
397 empty string or an empty list, depending on the context.
398
399 print '-' x 80; # print row of dashes
400
401 print "\t" x ($tab/8), ' ' x ($tab%8); # tab over
402
403 @ones = (1) x 80; # a list of 80 1's
404 @ones = (5) x @ones; # set all elements to 5
405
406 Additive Operators
407 Binary "+" returns the sum of two numbers.
408
409 Binary "-" returns the difference of two numbers.
410
411 Binary "." concatenates two strings.
412
413 Shift Operators
414 Binary "<<" returns the value of its left argument shifted left by the
415 number of bits specified by the right argument. Arguments should be
416 integers. (See also "Integer Arithmetic".)
417
418 Binary ">>" returns the value of its left argument shifted right by the
419 number of bits specified by the right argument. Arguments should be
420 integers. (See also "Integer Arithmetic".)
421
422 If "use integer" (see "Integer Arithmetic") is in force then signed C
423 integers are used (arithmetic shift), otherwise unsigned C integers are
424 used (logical shift), even for negative shiftees. In arithmetic right
425 shift the sign bit is replicated on the left, in logical shift zero
426 bits come in from the left.
427
428 Either way, the implementation isn't going to generate results larger
429 than the size of the integer type Perl was built with (32 bits or 64
430 bits).
431
432 Shifting by negative number of bits means the reverse shift: left shift
433 becomes right shift, right shift becomes left shift. This is unlike in
434 C, where negative shift is undefined.
435
436 Shifting by more bits than the size of the integers means most of the
437 time zero (all bits fall off), except that under "use integer" right
438 overshifting a negative shiftee results in -1. This is unlike in C,
439 where shifting by too many bits is undefined. A common C behavior is
440 "shift by modulo wordbits", so that for example
441
442 1 >> 64 == 1 >> (64 % 64) == 1 >> 0 == 1 # Common C behavior.
443
444 but that is completely accidental.
445
446 If you get tired of being subject to your platform's native integers,
447 the "use bigint" pragma neatly sidesteps the issue altogether:
448
449 print 20 << 20; # 20971520
450 print 20 << 40; # 5120 on 32-bit machines,
451 # 21990232555520 on 64-bit machines
452 use bigint;
453 print 20 << 100; # 25353012004564588029934064107520
454
455 Named Unary Operators
456 The various named unary operators are treated as functions with one
457 argument, with optional parentheses.
458
459 If any list operator ("print()", etc.) or any unary operator
460 ("chdir()", etc.) is followed by a left parenthesis as the next token,
461 the operator and arguments within parentheses are taken to be of
462 highest precedence, just like a normal function call. For example,
463 because named unary operators are higher precedence than "||":
464
465 chdir $foo || die; # (chdir $foo) || die
466 chdir($foo) || die; # (chdir $foo) || die
467 chdir ($foo) || die; # (chdir $foo) || die
468 chdir +($foo) || die; # (chdir $foo) || die
469
470 but, because "*" is higher precedence than named operators:
471
472 chdir $foo * 20; # chdir ($foo * 20)
473 chdir($foo) * 20; # (chdir $foo) * 20
474 chdir ($foo) * 20; # (chdir $foo) * 20
475 chdir +($foo) * 20; # chdir ($foo * 20)
476
477 rand 10 * 20; # rand (10 * 20)
478 rand(10) * 20; # (rand 10) * 20
479 rand (10) * 20; # (rand 10) * 20
480 rand +(10) * 20; # rand (10 * 20)
481
482 Regarding precedence, the filetest operators, like "-f", "-M", etc. are
483 treated like named unary operators, but they don't follow this
484 functional parenthesis rule. That means, for example, that
485 "-f($file).".bak"" is equivalent to "-f "$file.bak"".
486
487 See also "Terms and List Operators (Leftward)".
488
489 Relational Operators
490 Perl operators that return true or false generally return values that
491 can be safely used as numbers. For example, the relational operators
492 in this section and the equality operators in the next one return 1 for
493 true and a special version of the defined empty string, "", which
494 counts as a zero but is exempt from warnings about improper numeric
495 conversions, just as "0 but true" is.
496
497 Binary "<" returns true if the left argument is numerically less than
498 the right argument.
499
500 Binary ">" returns true if the left argument is numerically greater
501 than the right argument.
502
503 Binary "<=" returns true if the left argument is numerically less than
504 or equal to the right argument.
505
506 Binary ">=" returns true if the left argument is numerically greater
507 than or equal to the right argument.
508
509 Binary "lt" returns true if the left argument is stringwise less than
510 the right argument.
511
512 Binary "gt" returns true if the left argument is stringwise greater
513 than the right argument.
514
515 Binary "le" returns true if the left argument is stringwise less than
516 or equal to the right argument.
517
518 Binary "ge" returns true if the left argument is stringwise greater
519 than or equal to the right argument.
520
521 A sequence of relational operators, such as "$x < $y <= $z", performs
522 chained comparisons, in the manner described above in the section
523 "Operator Precedence and Associativity". Beware that they do not chain
524 with equality operators, which have lower precedence.
525
526 Equality Operators
527 Binary "==" returns true if the left argument is numerically equal to
528 the right argument.
529
530 Binary "!=" returns true if the left argument is numerically not equal
531 to the right argument.
532
533 Binary "eq" returns true if the left argument is stringwise equal to
534 the right argument.
535
536 Binary "ne" returns true if the left argument is stringwise not equal
537 to the right argument.
538
539 A sequence of the above equality operators, such as "$x == $y == $z",
540 performs chained comparisons, in the manner described above in the
541 section "Operator Precedence and Associativity". Beware that they do
542 not chain with relational operators, which have higher precedence.
543
544 Binary "<=>" returns -1, 0, or 1 depending on whether the left argument
545 is numerically less than, equal to, or greater than the right argument.
546 If your platform supports "NaN"'s (not-a-numbers) as numeric values,
547 using them with "<=>" returns undef. "NaN" is not "<", "==", ">", "<="
548 or ">=" anything (even "NaN"), so those 5 return false. "NaN != NaN"
549 returns true, as does "NaN !=" anything else. If your platform doesn't
550 support "NaN"'s then "NaN" is just a string with numeric value 0.
551
552 $ perl -le '$x = "NaN"; print "No NaN support here" if $x == $x'
553 $ perl -le '$x = "NaN"; print "NaN support here" if $x != $x'
554
555 (Note that the bigint, bigrat, and bignum pragmas all support "NaN".)
556
557 Binary "cmp" returns -1, 0, or 1 depending on whether the left argument
558 is stringwise less than, equal to, or greater than the right argument.
559
560 Binary "~~" does a smartmatch between its arguments. Smart matching is
561 described in the next section.
562
563 The two-sided ordering operators "<=>" and "cmp", and the smartmatch
564 operator "~~", are non-associative with respect to each other and with
565 respect to the equality operators of the same precedence.
566
567 "lt", "le", "ge", "gt" and "cmp" use the collation (sort) order
568 specified by the current "LC_COLLATE" locale if a "use locale" form
569 that includes collation is in effect. See perllocale. Do not mix
570 these with Unicode, only use them with legacy 8-bit locale encodings.
571 The standard "Unicode::Collate" and "Unicode::Collate::Locale" modules
572 offer much more powerful solutions to collation issues.
573
574 For case-insensitive comparisons, look at the "fc" in perlfunc case-
575 folding function, available in Perl v5.16 or later:
576
577 if ( fc($x) eq fc($y) ) { ... }
578
579 Class Instance Operator
580 Binary "isa" evaluates to true when the left argument is an object
581 instance of the class (or a subclass derived from that class) given by
582 the right argument. If the left argument is not defined, not a blessed
583 object instance, nor does not derive from the class given by the right
584 argument, the operator evaluates as false. The right argument may give
585 the class either as a bareword or a scalar expression that yields a
586 string class name:
587
588 if( $obj isa Some::Class ) { ... }
589
590 if( $obj isa "Different::Class" ) { ... }
591 if( $obj isa $name_of_class ) { ... }
592
593 This is an experimental feature and is available from Perl 5.31.6 when
594 enabled by "use feature 'isa'". It emits a warning in the
595 "experimental::isa" category.
596
597 Smartmatch Operator
598 First available in Perl 5.10.1 (the 5.10.0 version behaved
599 differently), binary "~~" does a "smartmatch" between its arguments.
600 This is mostly used implicitly in the "when" construct described in
601 perlsyn, although not all "when" clauses call the smartmatch operator.
602 Unique among all of Perl's operators, the smartmatch operator can
603 recurse. The smartmatch operator is experimental and its behavior is
604 subject to change.
605
606 It is also unique in that all other Perl operators impose a context
607 (usually string or numeric context) on their operands, autoconverting
608 those operands to those imposed contexts. In contrast, smartmatch
609 infers contexts from the actual types of its operands and uses that
610 type information to select a suitable comparison mechanism.
611
612 The "~~" operator compares its operands "polymorphically", determining
613 how to compare them according to their actual types (numeric, string,
614 array, hash, etc.). Like the equality operators with which it shares
615 the same precedence, "~~" returns 1 for true and "" for false. It is
616 often best read aloud as "in", "inside of", or "is contained in",
617 because the left operand is often looked for inside the right operand.
618 That makes the order of the operands to the smartmatch operand often
619 opposite that of the regular match operator. In other words, the
620 "smaller" thing is usually placed in the left operand and the larger
621 one in the right.
622
623 The behavior of a smartmatch depends on what type of things its
624 arguments are, as determined by the following table. The first row of
625 the table whose types apply determines the smartmatch behavior.
626 Because what actually happens is mostly determined by the type of the
627 second operand, the table is sorted on the right operand instead of on
628 the left.
629
630 Left Right Description and pseudocode
631 ===============================================================
632 Any undef check whether Any is undefined
633 like: !defined Any
634
635 Any Object invoke ~~ overloading on Object, or die
636
637 Right operand is an ARRAY:
638
639 Left Right Description and pseudocode
640 ===============================================================
641 ARRAY1 ARRAY2 recurse on paired elements of ARRAY1 and ARRAY2[2]
642 like: (ARRAY1[0] ~~ ARRAY2[0])
643 && (ARRAY1[1] ~~ ARRAY2[1]) && ...
644 HASH ARRAY any ARRAY elements exist as HASH keys
645 like: grep { exists HASH->{$_} } ARRAY
646 Regexp ARRAY any ARRAY elements pattern match Regexp
647 like: grep { /Regexp/ } ARRAY
648 undef ARRAY undef in ARRAY
649 like: grep { !defined } ARRAY
650 Any ARRAY smartmatch each ARRAY element[3]
651 like: grep { Any ~~ $_ } ARRAY
652
653 Right operand is a HASH:
654
655 Left Right Description and pseudocode
656 ===============================================================
657 HASH1 HASH2 all same keys in both HASHes
658 like: keys HASH1 ==
659 grep { exists HASH2->{$_} } keys HASH1
660 ARRAY HASH any ARRAY elements exist as HASH keys
661 like: grep { exists HASH->{$_} } ARRAY
662 Regexp HASH any HASH keys pattern match Regexp
663 like: grep { /Regexp/ } keys HASH
664 undef HASH always false (undef can't be a key)
665 like: 0 == 1
666 Any HASH HASH key existence
667 like: exists HASH->{Any}
668
669 Right operand is CODE:
670
671 Left Right Description and pseudocode
672 ===============================================================
673 ARRAY CODE sub returns true on all ARRAY elements[1]
674 like: !grep { !CODE->($_) } ARRAY
675 HASH CODE sub returns true on all HASH keys[1]
676 like: !grep { !CODE->($_) } keys HASH
677 Any CODE sub passed Any returns true
678 like: CODE->(Any)
679
680 Right operand is a Regexp:
681
682 Left Right Description and pseudocode
683 ===============================================================
684 ARRAY Regexp any ARRAY elements match Regexp
685 like: grep { /Regexp/ } ARRAY
686 HASH Regexp any HASH keys match Regexp
687 like: grep { /Regexp/ } keys HASH
688 Any Regexp pattern match
689 like: Any =~ /Regexp/
690
691 Other:
692
693 Left Right Description and pseudocode
694 ===============================================================
695 Object Any invoke ~~ overloading on Object,
696 or fall back to...
697
698 Any Num numeric equality
699 like: Any == Num
700 Num nummy[4] numeric equality
701 like: Num == nummy
702 undef Any check whether undefined
703 like: !defined(Any)
704 Any Any string equality
705 like: Any eq Any
706
707 Notes:
708
709 1. Empty hashes or arrays match.
710 2. That is, each element smartmatches the element of the same index in
711 the other array.[3]
712 3. If a circular reference is found, fall back to referential equality.
713 4. Either an actual number, or a string that looks like one.
714
715 The smartmatch implicitly dereferences any non-blessed hash or array
716 reference, so the "HASH" and "ARRAY" entries apply in those cases. For
717 blessed references, the "Object" entries apply. Smartmatches involving
718 hashes only consider hash keys, never hash values.
719
720 The "like" code entry is not always an exact rendition. For example,
721 the smartmatch operator short-circuits whenever possible, but "grep"
722 does not. Also, "grep" in scalar context returns the number of
723 matches, but "~~" returns only true or false.
724
725 Unlike most operators, the smartmatch operator knows to treat "undef"
726 specially:
727
728 use v5.10.1;
729 @array = (1, 2, 3, undef, 4, 5);
730 say "some elements undefined" if undef ~~ @array;
731
732 Each operand is considered in a modified scalar context, the
733 modification being that array and hash variables are passed by
734 reference to the operator, which implicitly dereferences them. Both
735 elements of each pair are the same:
736
737 use v5.10.1;
738
739 my %hash = (red => 1, blue => 2, green => 3,
740 orange => 4, yellow => 5, purple => 6,
741 black => 7, grey => 8, white => 9);
742
743 my @array = qw(red blue green);
744
745 say "some array elements in hash keys" if @array ~~ %hash;
746 say "some array elements in hash keys" if \@array ~~ \%hash;
747
748 say "red in array" if "red" ~~ @array;
749 say "red in array" if "red" ~~ \@array;
750
751 say "some keys end in e" if /e$/ ~~ %hash;
752 say "some keys end in e" if /e$/ ~~ \%hash;
753
754 Two arrays smartmatch if each element in the first array smartmatches
755 (that is, is "in") the corresponding element in the second array,
756 recursively.
757
758 use v5.10.1;
759 my @little = qw(red blue green);
760 my @bigger = ("red", "blue", [ "orange", "green" ] );
761 if (@little ~~ @bigger) { # true!
762 say "little is contained in bigger";
763 }
764
765 Because the smartmatch operator recurses on nested arrays, this will
766 still report that "red" is in the array.
767
768 use v5.10.1;
769 my @array = qw(red blue green);
770 my $nested_array = [[[[[[[ @array ]]]]]]];
771 say "red in array" if "red" ~~ $nested_array;
772
773 If two arrays smartmatch each other, then they are deep copies of each
774 others' values, as this example reports:
775
776 use v5.12.0;
777 my @a = (0, 1, 2, [3, [4, 5], 6], 7);
778 my @b = (0, 1, 2, [3, [4, 5], 6], 7);
779
780 if (@a ~~ @b && @b ~~ @a) {
781 say "a and b are deep copies of each other";
782 }
783 elsif (@a ~~ @b) {
784 say "a smartmatches in b";
785 }
786 elsif (@b ~~ @a) {
787 say "b smartmatches in a";
788 }
789 else {
790 say "a and b don't smartmatch each other at all";
791 }
792
793 If you were to set "$b[3] = 4", then instead of reporting that "a and b
794 are deep copies of each other", it now reports that "b smartmatches in
795 a". That's because the corresponding position in @a contains an array
796 that (eventually) has a 4 in it.
797
798 Smartmatching one hash against another reports whether both contain the
799 same keys, no more and no less. This could be used to see whether two
800 records have the same field names, without caring what values those
801 fields might have. For example:
802
803 use v5.10.1;
804 sub make_dogtag {
805 state $REQUIRED_FIELDS = { name=>1, rank=>1, serial_num=>1 };
806
807 my ($class, $init_fields) = @_;
808
809 die "Must supply (only) name, rank, and serial number"
810 unless $init_fields ~~ $REQUIRED_FIELDS;
811
812 ...
813 }
814
815 However, this only does what you mean if $init_fields is indeed a hash
816 reference. The condition "$init_fields ~~ $REQUIRED_FIELDS" also allows
817 the strings "name", "rank", "serial_num" as well as any array reference
818 that contains "name" or "rank" or "serial_num" anywhere to pass
819 through.
820
821 The smartmatch operator is most often used as the implicit operator of
822 a "when" clause. See the section on "Switch Statements" in perlsyn.
823
824 Smartmatching of Objects
825
826 To avoid relying on an object's underlying representation, if the
827 smartmatch's right operand is an object that doesn't overload "~~", it
828 raises the exception ""Smartmatching a non-overloaded object breaks
829 encapsulation"". That's because one has no business digging around to
830 see whether something is "in" an object. These are all illegal on
831 objects without a "~~" overload:
832
833 %hash ~~ $object
834 42 ~~ $object
835 "fred" ~~ $object
836
837 However, you can change the way an object is smartmatched by
838 overloading the "~~" operator. This is allowed to extend the usual
839 smartmatch semantics. For objects that do have an "~~" overload, see
840 overload.
841
842 Using an object as the left operand is allowed, although not very
843 useful. Smartmatching rules take precedence over overloading, so even
844 if the object in the left operand has smartmatch overloading, this will
845 be ignored. A left operand that is a non-overloaded object falls back
846 on a string or numeric comparison of whatever the "ref" operator
847 returns. That means that
848
849 $object ~~ X
850
851 does not invoke the overload method with "X" as an argument. Instead
852 the above table is consulted as normal, and based on the type of "X",
853 overloading may or may not be invoked. For simple strings or numbers,
854 "in" becomes equivalent to this:
855
856 $object ~~ $number ref($object) == $number
857 $object ~~ $string ref($object) eq $string
858
859 For example, this reports that the handle smells IOish (but please
860 don't really do this!):
861
862 use IO::Handle;
863 my $fh = IO::Handle->new();
864 if ($fh ~~ /\bIO\b/) {
865 say "handle smells IOish";
866 }
867
868 That's because it treats $fh as a string like
869 "IO::Handle=GLOB(0x8039e0)", then pattern matches against that.
870
871 Bitwise And
872 Binary "&" returns its operands ANDed together bit by bit. Although no
873 warning is currently raised, the result is not well defined when this
874 operation is performed on operands that aren't either numbers (see
875 "Integer Arithmetic") nor bitstrings (see "Bitwise String Operators").
876
877 Note that "&" has lower priority than relational operators, so for
878 example the parentheses are essential in a test like
879
880 print "Even\n" if ($x & 1) == 0;
881
882 If the "bitwise" feature is enabled via "use feature 'bitwise'" or "use
883 v5.28", then this operator always treats its operands as numbers.
884 Before Perl 5.28 this feature produced a warning in the
885 "experimental::bitwise" category.
886
887 Bitwise Or and Exclusive Or
888 Binary "|" returns its operands ORed together bit by bit.
889
890 Binary "^" returns its operands XORed together bit by bit.
891
892 Although no warning is currently raised, the results are not well
893 defined when these operations are performed on operands that aren't
894 either numbers (see "Integer Arithmetic") nor bitstrings (see "Bitwise
895 String Operators").
896
897 Note that "|" and "^" have lower priority than relational operators, so
898 for example the parentheses are essential in a test like
899
900 print "false\n" if (8 | 2) != 10;
901
902 If the "bitwise" feature is enabled via "use feature 'bitwise'" or "use
903 v5.28", then this operator always treats its operands as numbers.
904 Before Perl 5.28. this feature produced a warning in the
905 "experimental::bitwise" category.
906
907 C-style Logical And
908 Binary "&&" performs a short-circuit logical AND operation. That is,
909 if the left operand is false, the right operand is not even evaluated.
910 Scalar or list context propagates down to the right operand if it is
911 evaluated.
912
913 C-style Logical Or
914 Binary "||" performs a short-circuit logical OR operation. That is, if
915 the left operand is true, the right operand is not even evaluated.
916 Scalar or list context propagates down to the right operand if it is
917 evaluated.
918
919 Logical Defined-Or
920 Although it has no direct equivalent in C, Perl's "//" operator is
921 related to its C-style "or". In fact, it's exactly the same as "||",
922 except that it tests the left hand side's definedness instead of its
923 truth. Thus, "EXPR1 // EXPR2" returns the value of "EXPR1" if it's
924 defined, otherwise, the value of "EXPR2" is returned. ("EXPR1" is
925 evaluated in scalar context, "EXPR2" in the context of "//" itself).
926 Usually, this is the same result as "defined(EXPR1) ? EXPR1 : EXPR2"
927 (except that the ternary-operator form can be used as a lvalue, while
928 "EXPR1 // EXPR2" cannot). This is very useful for providing default
929 values for variables. If you actually want to test if at least one of
930 $x and $y is defined, use "defined($x // $y)".
931
932 The "||", "//" and "&&" operators return the last value evaluated
933 (unlike C's "||" and "&&", which return 0 or 1). Thus, a reasonably
934 portable way to find out the home directory might be:
935
936 $home = $ENV{HOME}
937 // $ENV{LOGDIR}
938 // (getpwuid($<))[7]
939 // die "You're homeless!\n";
940
941 In particular, this means that you shouldn't use this for selecting
942 between two aggregates for assignment:
943
944 @a = @b || @c; # This doesn't do the right thing
945 @a = scalar(@b) || @c; # because it really means this.
946 @a = @b ? @b : @c; # This works fine, though.
947
948 As alternatives to "&&" and "||" when used for control flow, Perl
949 provides the "and" and "or" operators (see below). The short-circuit
950 behavior is identical. The precedence of "and" and "or" is much lower,
951 however, so that you can safely use them after a list operator without
952 the need for parentheses:
953
954 unlink "alpha", "beta", "gamma"
955 or gripe(), next LINE;
956
957 With the C-style operators that would have been written like this:
958
959 unlink("alpha", "beta", "gamma")
960 || (gripe(), next LINE);
961
962 It would be even more readable to write that this way:
963
964 unless(unlink("alpha", "beta", "gamma")) {
965 gripe();
966 next LINE;
967 }
968
969 Using "or" for assignment is unlikely to do what you want; see below.
970
971 Range Operators
972 Binary ".." is the range operator, which is really two different
973 operators depending on the context. In list context, it returns a list
974 of values counting (up by ones) from the left value to the right value.
975 If the left value is greater than the right value then it returns the
976 empty list. The range operator is useful for writing "foreach (1..10)"
977 loops and for doing slice operations on arrays. In the current
978 implementation, no temporary array is created when the range operator
979 is used as the expression in "foreach" loops, but older versions of
980 Perl might burn a lot of memory when you write something like this:
981
982 for (1 .. 1_000_000) {
983 # code
984 }
985
986 The range operator also works on strings, using the magical auto-
987 increment, see below.
988
989 In scalar context, ".." returns a boolean value. The operator is
990 bistable, like a flip-flop, and emulates the line-range (comma)
991 operator of sed, awk, and various editors. Each ".." operator
992 maintains its own boolean state, even across calls to a subroutine that
993 contains it. It is false as long as its left operand is false. Once
994 the left operand is true, the range operator stays true until the right
995 operand is true, AFTER which the range operator becomes false again.
996 It doesn't become false till the next time the range operator is
997 evaluated. It can test the right operand and become false on the same
998 evaluation it became true (as in awk), but it still returns true once.
999 If you don't want it to test the right operand until the next
1000 evaluation, as in sed, just use three dots ("...") instead of two. In
1001 all other regards, "..." behaves just like ".." does.
1002
1003 The right operand is not evaluated while the operator is in the "false"
1004 state, and the left operand is not evaluated while the operator is in
1005 the "true" state. The precedence is a little lower than || and &&.
1006 The value returned is either the empty string for false, or a sequence
1007 number (beginning with 1) for true. The sequence number is reset for
1008 each range encountered. The final sequence number in a range has the
1009 string "E0" appended to it, which doesn't affect its numeric value, but
1010 gives you something to search for if you want to exclude the endpoint.
1011 You can exclude the beginning point by waiting for the sequence number
1012 to be greater than 1.
1013
1014 If either operand of scalar ".." is a constant expression, that operand
1015 is considered true if it is equal ("==") to the current input line
1016 number (the $. variable).
1017
1018 To be pedantic, the comparison is actually "int(EXPR) == int(EXPR)",
1019 but that is only an issue if you use a floating point expression; when
1020 implicitly using $. as described in the previous paragraph, the
1021 comparison is "int(EXPR) == int($.)" which is only an issue when $. is
1022 set to a floating point value and you are not reading from a file.
1023 Furthermore, "span" .. "spat" or "2.18 .. 3.14" will not do what you
1024 want in scalar context because each of the operands are evaluated using
1025 their integer representation.
1026
1027 Examples:
1028
1029 As a scalar operator:
1030
1031 if (101 .. 200) { print; } # print 2nd hundred lines, short for
1032 # if ($. == 101 .. $. == 200) { print; }
1033
1034 next LINE if (1 .. /^$/); # skip header lines, short for
1035 # next LINE if ($. == 1 .. /^$/);
1036 # (typically in a loop labeled LINE)
1037
1038 s/^/> / if (/^$/ .. eof()); # quote body
1039
1040 # parse mail messages
1041 while (<>) {
1042 $in_header = 1 .. /^$/;
1043 $in_body = /^$/ .. eof;
1044 if ($in_header) {
1045 # do something
1046 } else { # in body
1047 # do something else
1048 }
1049 } continue {
1050 close ARGV if eof; # reset $. each file
1051 }
1052
1053 Here's a simple example to illustrate the difference between the two
1054 range operators:
1055
1056 @lines = (" - Foo",
1057 "01 - Bar",
1058 "1 - Baz",
1059 " - Quux");
1060
1061 foreach (@lines) {
1062 if (/0/ .. /1/) {
1063 print "$_\n";
1064 }
1065 }
1066
1067 This program will print only the line containing "Bar". If the range
1068 operator is changed to "...", it will also print the "Baz" line.
1069
1070 And now some examples as a list operator:
1071
1072 for (101 .. 200) { print } # print $_ 100 times
1073 @foo = @foo[0 .. $#foo]; # an expensive no-op
1074 @foo = @foo[$#foo-4 .. $#foo]; # slice last 5 items
1075
1076 Because each operand is evaluated in integer form, "2.18 .. 3.14" will
1077 return two elements in list context.
1078
1079 @list = (2.18 .. 3.14); # same as @list = (2 .. 3);
1080
1081 The range operator in list context can make use of the magical auto-
1082 increment algorithm if both operands are strings, subject to the
1083 following rules:
1084
1085 • With one exception (below), if both strings look like numbers to
1086 Perl, the magic increment will not be applied, and the strings will
1087 be treated as numbers (more specifically, integers) instead.
1088
1089 For example, "-2".."2" is the same as "-2..2", and "2.18".."3.14"
1090 produces "2, 3".
1091
1092 • The exception to the above rule is when the left-hand string begins
1093 with 0 and is longer than one character, in this case the magic
1094 increment will be applied, even though strings like "01" would
1095 normally look like a number to Perl.
1096
1097 For example, "01".."04" produces "01", "02", "03", "04", and
1098 "00".."-1" produces "00" through "99" - this may seem surprising,
1099 but see the following rules for why it works this way. To get
1100 dates with leading zeros, you can say:
1101
1102 @z2 = ("01" .. "31");
1103 print $z2[$mday];
1104
1105 If you want to force strings to be interpreted as numbers, you
1106 could say
1107
1108 @numbers = ( 0+$first .. 0+$last );
1109
1110 Note: In Perl versions 5.30 and below, any string on the left-hand
1111 side beginning with "0", including the string "0" itself, would
1112 cause the magic string increment behavior. This means that on these
1113 Perl versions, "0".."-1" would produce "0" through "99", which was
1114 inconsistent with "0..-1", which produces the empty list. This also
1115 means that "0".."9" now produces a list of integers instead of a
1116 list of strings.
1117
1118 • If the initial value specified isn't part of a magical increment
1119 sequence (that is, a non-empty string matching
1120 "/^[a-zA-Z]*[0-9]*\z/"), only the initial value will be returned.
1121
1122 For example, "ax".."az" produces "ax", "ay", "az", but "*x".."az"
1123 produces only "*x".
1124
1125 • For other initial values that are strings that do follow the rules
1126 of the magical increment, the corresponding sequence will be
1127 returned.
1128
1129 For example, you can say
1130
1131 @alphabet = ("A" .. "Z");
1132
1133 to get all normal letters of the English alphabet, or
1134
1135 $hexdigit = (0 .. 9, "a" .. "f")[$num & 15];
1136
1137 to get a hexadecimal digit.
1138
1139 • If the final value specified is not in the sequence that the
1140 magical increment would produce, the sequence goes until the next
1141 value would be longer than the final value specified. If the length
1142 of the final string is shorter than the first, the empty list is
1143 returned.
1144
1145 For example, "a".."--" is the same as "a".."zz", "0".."xx" produces
1146 "0" through "99", and "aaa".."--" returns the empty list.
1147
1148 As of Perl 5.26, the list-context range operator on strings works as
1149 expected in the scope of "use feature 'unicode_strings". In previous
1150 versions, and outside the scope of that feature, it exhibits "The
1151 "Unicode Bug"" in perlunicode: its behavior depends on the internal
1152 encoding of the range endpoint.
1153
1154 Because the magical increment only works on non-empty strings matching
1155 "/^[a-zA-Z]*[0-9]*\z/", the following will only return an alpha:
1156
1157 use charnames "greek";
1158 my @greek_small = ("\N{alpha}" .. "\N{omega}");
1159
1160 To get the 25 traditional lowercase Greek letters, including both
1161 sigmas, you could use this instead:
1162
1163 use charnames "greek";
1164 my @greek_small = map { chr } ( ord("\N{alpha}")
1165 ..
1166 ord("\N{omega}")
1167 );
1168
1169 However, because there are many other lowercase Greek characters than
1170 just those, to match lowercase Greek characters in a regular
1171 expression, you could use the pattern "/(?:(?=\p{Greek})\p{Lower})+/"
1172 (or the experimental feature "/(?[ \p{Greek} & \p{Lower} ])+/").
1173
1174 Conditional Operator
1175 Ternary "?:" is the conditional operator, just as in C. It works much
1176 like an if-then-else. If the argument before the "?" is true, the
1177 argument before the ":" is returned, otherwise the argument after the
1178 ":" is returned. For example:
1179
1180 printf "I have %d dog%s.\n", $n,
1181 ($n == 1) ? "" : "s";
1182
1183 Scalar or list context propagates downward into the 2nd or 3rd
1184 argument, whichever is selected.
1185
1186 $x = $ok ? $y : $z; # get a scalar
1187 @x = $ok ? @y : @z; # get an array
1188 $x = $ok ? @y : @z; # oops, that's just a count!
1189
1190 The operator may be assigned to if both the 2nd and 3rd arguments are
1191 legal lvalues (meaning that you can assign to them):
1192
1193 ($x_or_y ? $x : $y) = $z;
1194
1195 Because this operator produces an assignable result, using assignments
1196 without parentheses will get you in trouble. For example, this:
1197
1198 $x % 2 ? $x += 10 : $x += 2
1199
1200 Really means this:
1201
1202 (($x % 2) ? ($x += 10) : $x) += 2
1203
1204 Rather than this:
1205
1206 ($x % 2) ? ($x += 10) : ($x += 2)
1207
1208 That should probably be written more simply as:
1209
1210 $x += ($x % 2) ? 10 : 2;
1211
1212 Assignment Operators
1213 "=" is the ordinary assignment operator.
1214
1215 Assignment operators work as in C. That is,
1216
1217 $x += 2;
1218
1219 is equivalent to
1220
1221 $x = $x + 2;
1222
1223 although without duplicating any side effects that dereferencing the
1224 lvalue might trigger, such as from "tie()". Other assignment operators
1225 work similarly. The following are recognized:
1226
1227 **= += *= &= &.= <<= &&=
1228 -= /= |= |.= >>= ||=
1229 .= %= ^= ^.= //=
1230 x=
1231
1232 Although these are grouped by family, they all have the precedence of
1233 assignment. These combined assignment operators can only operate on
1234 scalars, whereas the ordinary assignment operator can assign to arrays,
1235 hashes, lists and even references. (See "Context" and "List value
1236 constructors" in perldata, and "Assigning to References" in perlref.)
1237
1238 Unlike in C, the scalar assignment operator produces a valid lvalue.
1239 Modifying an assignment is equivalent to doing the assignment and then
1240 modifying the variable that was assigned to. This is useful for
1241 modifying a copy of something, like this:
1242
1243 ($tmp = $global) =~ tr/13579/24680/;
1244
1245 Although as of 5.14, that can be also be accomplished this way:
1246
1247 use v5.14;
1248 $tmp = ($global =~ tr/13579/24680/r);
1249
1250 Likewise,
1251
1252 ($x += 2) *= 3;
1253
1254 is equivalent to
1255
1256 $x += 2;
1257 $x *= 3;
1258
1259 Similarly, a list assignment in list context produces the list of
1260 lvalues assigned to, and a list assignment in scalar context returns
1261 the number of elements produced by the expression on the right hand
1262 side of the assignment.
1263
1264 The three dotted bitwise assignment operators ("&.=" "|.=" "^.=") are
1265 new in Perl 5.22. See "Bitwise String Operators".
1266
1267 Comma Operator
1268 Binary "," is the comma operator. In scalar context it evaluates its
1269 left argument, throws that value away, then evaluates its right
1270 argument and returns that value. This is just like C's comma operator.
1271
1272 In list context, it's just the list argument separator, and inserts
1273 both its arguments into the list. These arguments are also evaluated
1274 from left to right.
1275
1276 The "=>" operator (sometimes pronounced "fat comma") is a synonym for
1277 the comma except that it causes a word on its left to be interpreted as
1278 a string if it begins with a letter or underscore and is composed only
1279 of letters, digits and underscores. This includes operands that might
1280 otherwise be interpreted as operators, constants, single number
1281 v-strings or function calls. If in doubt about this behavior, the left
1282 operand can be quoted explicitly.
1283
1284 Otherwise, the "=>" operator behaves exactly as the comma operator or
1285 list argument separator, according to context.
1286
1287 For example:
1288
1289 use constant FOO => "something";
1290
1291 my %h = ( FOO => 23 );
1292
1293 is equivalent to:
1294
1295 my %h = ("FOO", 23);
1296
1297 It is NOT:
1298
1299 my %h = ("something", 23);
1300
1301 The "=>" operator is helpful in documenting the correspondence between
1302 keys and values in hashes, and other paired elements in lists.
1303
1304 %hash = ( $key => $value );
1305 login( $username => $password );
1306
1307 The special quoting behavior ignores precedence, and hence may apply to
1308 part of the left operand:
1309
1310 print time.shift => "bbb";
1311
1312 That example prints something like "1314363215shiftbbb", because the
1313 "=>" implicitly quotes the "shift" immediately on its left, ignoring
1314 the fact that "time.shift" is the entire left operand.
1315
1316 List Operators (Rightward)
1317 On the right side of a list operator, the comma has very low
1318 precedence, such that it controls all comma-separated expressions found
1319 there. The only operators with lower precedence are the logical
1320 operators "and", "or", and "not", which may be used to evaluate calls
1321 to list operators without the need for parentheses:
1322
1323 open HANDLE, "< :encoding(UTF-8)", "filename"
1324 or die "Can't open: $!\n";
1325
1326 However, some people find that code harder to read than writing it with
1327 parentheses:
1328
1329 open(HANDLE, "< :encoding(UTF-8)", "filename")
1330 or die "Can't open: $!\n";
1331
1332 in which case you might as well just use the more customary "||"
1333 operator:
1334
1335 open(HANDLE, "< :encoding(UTF-8)", "filename")
1336 || die "Can't open: $!\n";
1337
1338 See also discussion of list operators in "Terms and List Operators
1339 (Leftward)".
1340
1341 Logical Not
1342 Unary "not" returns the logical negation of the expression to its
1343 right. It's the equivalent of "!" except for the very low precedence.
1344
1345 Logical And
1346 Binary "and" returns the logical conjunction of the two surrounding
1347 expressions. It's equivalent to "&&" except for the very low
1348 precedence. This means that it short-circuits: the right expression is
1349 evaluated only if the left expression is true.
1350
1351 Logical or and Exclusive Or
1352 Binary "or" returns the logical disjunction of the two surrounding
1353 expressions. It's equivalent to "||" except for the very low
1354 precedence. This makes it useful for control flow:
1355
1356 print FH $data or die "Can't write to FH: $!";
1357
1358 This means that it short-circuits: the right expression is evaluated
1359 only if the left expression is false. Due to its precedence, you must
1360 be careful to avoid using it as replacement for the "||" operator. It
1361 usually works out better for flow control than in assignments:
1362
1363 $x = $y or $z; # bug: this is wrong
1364 ($x = $y) or $z; # really means this
1365 $x = $y || $z; # better written this way
1366
1367 However, when it's a list-context assignment and you're trying to use
1368 "||" for control flow, you probably need "or" so that the assignment
1369 takes higher precedence.
1370
1371 @info = stat($file) || die; # oops, scalar sense of stat!
1372 @info = stat($file) or die; # better, now @info gets its due
1373
1374 Then again, you could always use parentheses.
1375
1376 Binary "xor" returns the exclusive-OR of the two surrounding
1377 expressions. It cannot short-circuit (of course).
1378
1379 There is no low precedence operator for defined-OR.
1380
1381 C Operators Missing From Perl
1382 Here is what C has that Perl doesn't:
1383
1384 unary & Address-of operator. (But see the "\" operator for taking a
1385 reference.)
1386
1387 unary * Dereference-address operator. (Perl's prefix dereferencing
1388 operators are typed: "$", "@", "%", and "&".)
1389
1390 (TYPE) Type-casting operator.
1391
1392 Quote and Quote-like Operators
1393 While we usually think of quotes as literal values, in Perl they
1394 function as operators, providing various kinds of interpolating and
1395 pattern matching capabilities. Perl provides customary quote
1396 characters for these behaviors, but also provides a way for you to
1397 choose your quote character for any of them. In the following table, a
1398 "{}" represents any pair of delimiters you choose.
1399
1400 Customary Generic Meaning Interpolates
1401 '' q{} Literal no
1402 "" qq{} Literal yes
1403 `` qx{} Command yes*
1404 qw{} Word list no
1405 // m{} Pattern match yes*
1406 qr{} Pattern yes*
1407 s{}{} Substitution yes*
1408 tr{}{} Transliteration no (but see below)
1409 y{}{} Transliteration no (but see below)
1410 <<EOF here-doc yes*
1411
1412 * unless the delimiter is ''.
1413
1414 Non-bracketing delimiters use the same character fore and aft, but the
1415 four sorts of ASCII brackets (round, angle, square, curly) all nest,
1416 which means that
1417
1418 q{foo{bar}baz}
1419
1420 is the same as
1421
1422 'foo{bar}baz'
1423
1424 Note, however, that this does not always work for quoting Perl code:
1425
1426 $s = q{ if($x eq "}") ... }; # WRONG
1427
1428 is a syntax error. The "Text::Balanced" module (standard as of v5.8,
1429 and from CPAN before then) is able to do this properly.
1430
1431 There can (and in some cases, must) be whitespace between the operator
1432 and the quoting characters, except when "#" is being used as the
1433 quoting character. "q#foo#" is parsed as the string "foo", while
1434 "q #foo#" is the operator "q" followed by a comment. Its argument will
1435 be taken from the next line. This allows you to write:
1436
1437 s {foo} # Replace foo
1438 {bar} # with bar.
1439
1440 The cases where whitespace must be used are when the quoting character
1441 is a word character (meaning it matches "/\w/"):
1442
1443 q XfooX # Works: means the string 'foo'
1444 qXfooX # WRONG!
1445
1446 The following escape sequences are available in constructs that
1447 interpolate, and in transliterations whose delimiters aren't single
1448 quotes ("'"). In all the ones with braces, any number of blanks and/or
1449 tabs adjoining and within the braces are allowed (and ignored).
1450
1451 Sequence Note Description
1452 \t tab (HT, TAB)
1453 \n newline (NL)
1454 \r return (CR)
1455 \f form feed (FF)
1456 \b backspace (BS)
1457 \a alarm (bell) (BEL)
1458 \e escape (ESC)
1459 \x{263A} [1,8] hex char (example shown: SMILEY)
1460 \x{ 263A } Same, but shows optional blanks inside and
1461 adjoining the braces
1462 \x1b [2,8] restricted range hex char (example: ESC)
1463 \N{name} [3] named Unicode character or character sequence
1464 \N{U+263D} [4,8] Unicode character (example: FIRST QUARTER MOON)
1465 \c[ [5] control char (example: chr(27))
1466 \o{23072} [6,8] octal char (example: SMILEY)
1467 \033 [7,8] restricted range octal char (example: ESC)
1468
1469 Note that any escape sequence using braces inside interpolated
1470 constructs may have optional blanks (tab or space characters) adjoining
1471 with and inside of the braces, as illustrated above by the second
1472 "\x{ }" example.
1473
1474 [1] The result is the character specified by the hexadecimal number
1475 between the braces. See "[8]" below for details on which
1476 character.
1477
1478 Blanks (tab or space characters) may separate the number from
1479 either or both of the braces.
1480
1481 Otherwise, only hexadecimal digits are valid between the braces.
1482 If an invalid character is encountered, a warning will be issued
1483 and the invalid character and all subsequent characters (valid or
1484 invalid) within the braces will be discarded.
1485
1486 If there are no valid digits between the braces, the generated
1487 character is the NULL character ("\x{00}"). However, an explicit
1488 empty brace ("\x{}") will not cause a warning (currently).
1489
1490 [2] The result is the character specified by the hexadecimal number in
1491 the range 0x00 to 0xFF. See "[8]" below for details on which
1492 character.
1493
1494 Only hexadecimal digits are valid following "\x". When "\x" is
1495 followed by fewer than two valid digits, any valid digits will be
1496 zero-padded. This means that "\x7" will be interpreted as "\x07",
1497 and a lone "\x" will be interpreted as "\x00". Except at the end
1498 of a string, having fewer than two valid digits will result in a
1499 warning. Note that although the warning says the illegal character
1500 is ignored, it is only ignored as part of the escape and will still
1501 be used as the subsequent character in the string. For example:
1502
1503 Original Result Warns?
1504 "\x7" "\x07" no
1505 "\x" "\x00" no
1506 "\x7q" "\x07q" yes
1507 "\xq" "\x00q" yes
1508
1509 [3] The result is the Unicode character or character sequence given by
1510 name. See charnames.
1511
1512 [4] "\N{U+hexadecimal number}" means the Unicode character whose
1513 Unicode code point is hexadecimal number.
1514
1515 [5] The character following "\c" is mapped to some other character as
1516 shown in the table:
1517
1518 Sequence Value
1519 \c@ chr(0)
1520 \cA chr(1)
1521 \ca chr(1)
1522 \cB chr(2)
1523 \cb chr(2)
1524 ...
1525 \cZ chr(26)
1526 \cz chr(26)
1527 \c[ chr(27)
1528 # See below for chr(28)
1529 \c] chr(29)
1530 \c^ chr(30)
1531 \c_ chr(31)
1532 \c? chr(127) # (on ASCII platforms; see below for link to
1533 # EBCDIC discussion)
1534
1535 In other words, it's the character whose code point has had 64
1536 xor'd with its uppercase. "\c?" is DELETE on ASCII platforms
1537 because "ord("?") ^ 64" is 127, and "\c@" is NULL because the ord
1538 of "@" is 64, so xor'ing 64 itself produces 0.
1539
1540 Also, "\c\X" yields " chr(28) . "X"" for any X, but cannot come at
1541 the end of a string, because the backslash would be parsed as
1542 escaping the end quote.
1543
1544 On ASCII platforms, the resulting characters from the list above
1545 are the complete set of ASCII controls. This isn't the case on
1546 EBCDIC platforms; see "OPERATOR DIFFERENCES" in perlebcdic for a
1547 full discussion of the differences between these for ASCII versus
1548 EBCDIC platforms.
1549
1550 Use of any other character following the "c" besides those listed
1551 above is discouraged, and as of Perl v5.20, the only characters
1552 actually allowed are the printable ASCII ones, minus the left brace
1553 "{". What happens for any of the allowed other characters is that
1554 the value is derived by xor'ing with the seventh bit, which is 64,
1555 and a warning raised if enabled. Using the non-allowed characters
1556 generates a fatal error.
1557
1558 To get platform independent controls, you can use "\N{...}".
1559
1560 [6] The result is the character specified by the octal number between
1561 the braces. See "[8]" below for details on which character.
1562
1563 Blanks (tab or space characters) may separate the number from
1564 either or both of the braces.
1565
1566 Otherwise, if a character that isn't an octal digit is encountered,
1567 a warning is raised, and the value is based on the octal digits
1568 before it, discarding it and all following characters up to the
1569 closing brace. It is a fatal error if there are no octal digits at
1570 all.
1571
1572 [7] The result is the character specified by the three-digit octal
1573 number in the range 000 to 777 (but best to not use above 077, see
1574 next paragraph). See "[8]" below for details on which character.
1575
1576 Some contexts allow 2 or even 1 digit, but any usage without
1577 exactly three digits, the first being a zero, may give unintended
1578 results. (For example, in a regular expression it may be confused
1579 with a backreference; see "Octal escapes" in perlrebackslash.)
1580 Starting in Perl 5.14, you may use "\o{}" instead, which avoids all
1581 these problems. Otherwise, it is best to use this construct only
1582 for ordinals "\077" and below, remembering to pad to the left with
1583 zeros to make three digits. For larger ordinals, either use
1584 "\o{}", or convert to something else, such as to hex and use
1585 "\N{U+}" (which is portable between platforms with different
1586 character sets) or "\x{}" instead.
1587
1588 [8] Several constructs above specify a character by a number. That
1589 number gives the character's position in the character set encoding
1590 (indexed from 0). This is called synonymously its ordinal, code
1591 position, or code point. Perl works on platforms that have a
1592 native encoding currently of either ASCII/Latin1 or EBCDIC, each of
1593 which allow specification of 256 characters. In general, if the
1594 number is 255 (0xFF, 0377) or below, Perl interprets this in the
1595 platform's native encoding. If the number is 256 (0x100, 0400) or
1596 above, Perl interprets it as a Unicode code point and the result is
1597 the corresponding Unicode character. For example "\x{50}" and
1598 "\o{120}" both are the number 80 in decimal, which is less than
1599 256, so the number is interpreted in the native character set
1600 encoding. In ASCII the character in the 80th position (indexed
1601 from 0) is the letter "P", and in EBCDIC it is the ampersand symbol
1602 "&". "\x{100}" and "\o{400}" are both 256 in decimal, so the
1603 number is interpreted as a Unicode code point no matter what the
1604 native encoding is. The name of the character in the 256th
1605 position (indexed by 0) in Unicode is "LATIN CAPITAL LETTER A WITH
1606 MACRON".
1607
1608 An exception to the above rule is that "\N{U+hex number}" is always
1609 interpreted as a Unicode code point, so that "\N{U+0050}" is "P"
1610 even on EBCDIC platforms.
1611
1612 NOTE: Unlike C and other languages, Perl has no "\v" escape sequence
1613 for the vertical tab (VT, which is 11 in both ASCII and EBCDIC), but
1614 you may use "\N{VT}", "\ck", "\N{U+0b}", or "\x0b". ("\v" does have
1615 meaning in regular expression patterns in Perl, see perlre.)
1616
1617 The following escape sequences are available in constructs that
1618 interpolate, but not in transliterations.
1619
1620 \l lowercase next character only
1621 \u titlecase (not uppercase!) next character only
1622 \L lowercase all characters till \E or end of string
1623 \U uppercase all characters till \E or end of string
1624 \F foldcase all characters till \E or end of string
1625 \Q quote (disable) pattern metacharacters till \E or
1626 end of string
1627 \E end either case modification or quoted section
1628 (whichever was last seen)
1629
1630 See "quotemeta" in perlfunc for the exact definition of characters that
1631 are quoted by "\Q".
1632
1633 "\L", "\U", "\F", and "\Q" can stack, in which case you need one "\E"
1634 for each. For example:
1635
1636 say"This \Qquoting \ubusiness \Uhere isn't quite\E done yet,\E is it?";
1637 This quoting\ Business\ HERE\ ISN\'T\ QUITE\ done\ yet\, is it?
1638
1639 If a "use locale" form that includes "LC_CTYPE" is in effect (see
1640 perllocale), the case map used by "\l", "\L", "\u", and "\U" is taken
1641 from the current locale. If Unicode (for example, "\N{}" or code
1642 points of 0x100 or beyond) is being used, the case map used by "\l",
1643 "\L", "\u", and "\U" is as defined by Unicode. That means that case-
1644 mapping a single character can sometimes produce a sequence of several
1645 characters. Under "use locale", "\F" produces the same results as "\L"
1646 for all locales but a UTF-8 one, where it instead uses the Unicode
1647 definition.
1648
1649 All systems use the virtual "\n" to represent a line terminator, called
1650 a "newline". There is no such thing as an unvarying, physical newline
1651 character. It is only an illusion that the operating system, device
1652 drivers, C libraries, and Perl all conspire to preserve. Not all
1653 systems read "\r" as ASCII CR and "\n" as ASCII LF. For example, on
1654 the ancient Macs (pre-MacOS X) of yesteryear, these used to be
1655 reversed, and on systems without a line terminator, printing "\n" might
1656 emit no actual data. In general, use "\n" when you mean a "newline"
1657 for your system, but use the literal ASCII when you need an exact
1658 character. For example, most networking protocols expect and prefer a
1659 CR+LF ("\015\012" or "\cM\cJ") for line terminators, and although they
1660 often accept just "\012", they seldom tolerate just "\015". If you get
1661 in the habit of using "\n" for networking, you may be burned some day.
1662
1663 For constructs that do interpolate, variables beginning with ""$"" or
1664 ""@"" are interpolated. Subscripted variables such as $a[3] or
1665 "$href->{key}[0]" are also interpolated, as are array and hash slices.
1666 But method calls such as "$obj->meth" are not.
1667
1668 Interpolating an array or slice interpolates the elements in order,
1669 separated by the value of $", so is equivalent to interpolating
1670 "join $", @array". "Punctuation" arrays such as "@*" are usually
1671 interpolated only if the name is enclosed in braces "@{*}", but the
1672 arrays @_, "@+", and "@-" are interpolated even without braces.
1673
1674 For double-quoted strings, the quoting from "\Q" is applied after
1675 interpolation and escapes are processed.
1676
1677 "abc\Qfoo\tbar$s\Exyz"
1678
1679 is equivalent to
1680
1681 "abc" . quotemeta("foo\tbar$s") . "xyz"
1682
1683 For the pattern of regex operators ("qr//", "m//" and "s///"), the
1684 quoting from "\Q" is applied after interpolation is processed, but
1685 before escapes are processed. This allows the pattern to match
1686 literally (except for "$" and "@"). For example, the following
1687 matches:
1688
1689 '\s\t' =~ /\Q\s\t/
1690
1691 Because "$" or "@" trigger interpolation, you'll need to use something
1692 like "/\Quser\E\@\Qhost/" to match them literally.
1693
1694 Patterns are subject to an additional level of interpretation as a
1695 regular expression. This is done as a second pass, after variables are
1696 interpolated, so that regular expressions may be incorporated into the
1697 pattern from the variables. If this is not what you want, use "\Q" to
1698 interpolate a variable literally.
1699
1700 Apart from the behavior described above, Perl does not expand multiple
1701 levels of interpolation. In particular, contrary to the expectations
1702 of shell programmers, back-quotes do NOT interpolate within double
1703 quotes, nor do single quotes impede evaluation of variables when used
1704 within double quotes.
1705
1706 Regexp Quote-Like Operators
1707 Here are the quote-like operators that apply to pattern matching and
1708 related activities.
1709
1710 "qr/STRING/msixpodualn"
1711 This operator quotes (and possibly compiles) its STRING as a
1712 regular expression. STRING is interpolated the same way as
1713 PATTERN in "m/PATTERN/". If "'" is used as the delimiter, no
1714 variable interpolation is done. Returns a Perl value which may
1715 be used instead of the corresponding "/STRING/msixpodualn"
1716 expression. The returned value is a normalized version of the
1717 original pattern. It magically differs from a string
1718 containing the same characters: "ref(qr/x/)" returns "Regexp";
1719 however, dereferencing it is not well defined (you currently
1720 get the normalized version of the original pattern, but this
1721 may change).
1722
1723 For example,
1724
1725 $rex = qr/my.STRING/is;
1726 print $rex; # prints (?si-xm:my.STRING)
1727 s/$rex/foo/;
1728
1729 is equivalent to
1730
1731 s/my.STRING/foo/is;
1732
1733 The result may be used as a subpattern in a match:
1734
1735 $re = qr/$pattern/;
1736 $string =~ /foo${re}bar/; # can be interpolated in other
1737 # patterns
1738 $string =~ $re; # or used standalone
1739 $string =~ /$re/; # or this way
1740
1741 Since Perl may compile the pattern at the moment of execution
1742 of the "qr()" operator, using "qr()" may have speed advantages
1743 in some situations, notably if the result of "qr()" is used
1744 standalone:
1745
1746 sub match {
1747 my $patterns = shift;
1748 my @compiled = map qr/$_/i, @$patterns;
1749 grep {
1750 my $success = 0;
1751 foreach my $pat (@compiled) {
1752 $success = 1, last if /$pat/;
1753 }
1754 $success;
1755 } @_;
1756 }
1757
1758 Precompilation of the pattern into an internal representation
1759 at the moment of "qr()" avoids the need to recompile the
1760 pattern every time a match "/$pat/" is attempted. (Perl has
1761 many other internal optimizations, but none would be triggered
1762 in the above example if we did not use "qr()" operator.)
1763
1764 Options (specified by the following modifiers) are:
1765
1766 m Treat string as multiple lines.
1767 s Treat string as single line. (Make . match a newline)
1768 i Do case-insensitive pattern matching.
1769 x Use extended regular expressions; specifying two
1770 x's means \t and the SPACE character are ignored within
1771 square-bracketed character classes
1772 p When matching preserve a copy of the matched string so
1773 that ${^PREMATCH}, ${^MATCH}, ${^POSTMATCH} will be
1774 defined (ignored starting in v5.20) as these are always
1775 defined starting in that release
1776 o Compile pattern only once.
1777 a ASCII-restrict: Use ASCII for \d, \s, \w and [[:posix:]]
1778 character classes; specifying two a's adds the further
1779 restriction that no ASCII character will match a
1780 non-ASCII one under /i.
1781 l Use the current run-time locale's rules.
1782 u Use Unicode rules.
1783 d Use Unicode or native charset, as in 5.12 and earlier.
1784 n Non-capture mode. Don't let () fill in $1, $2, etc...
1785
1786 If a precompiled pattern is embedded in a larger pattern then
1787 the effect of "msixpluadn" will be propagated appropriately.
1788 The effect that the "/o" modifier has is not propagated, being
1789 restricted to those patterns explicitly using it.
1790
1791 The "/a", "/d", "/l", and "/u" modifiers (added in Perl 5.14)
1792 control the character set rules, but "/a" is the only one you
1793 are likely to want to specify explicitly; the other three are
1794 selected automatically by various pragmas.
1795
1796 See perlre for additional information on valid syntax for
1797 STRING, and for a detailed look at the semantics of regular
1798 expressions. In particular, all modifiers except the largely
1799 obsolete "/o" are further explained in "Modifiers" in perlre.
1800 "/o" is described in the next section.
1801
1802 "m/PATTERN/msixpodualngc"
1803 "/PATTERN/msixpodualngc"
1804 Searches a string for a pattern match, and in scalar context
1805 returns true if it succeeds, false if it fails. If no string
1806 is specified via the "=~" or "!~" operator, the $_ string is
1807 searched. (The string specified with "=~" need not be an
1808 lvalue--it may be the result of an expression evaluation, but
1809 remember the "=~" binds rather tightly.) See also perlre.
1810
1811 Options are as described in "qr//" above; in addition, the
1812 following match process modifiers are available:
1813
1814 g Match globally, i.e., find all occurrences.
1815 c Do not reset search position on a failed match when /g is
1816 in effect.
1817
1818 If "/" is the delimiter then the initial "m" is optional. With
1819 the "m" you can use any pair of non-whitespace (ASCII)
1820 characters as delimiters. This is particularly useful for
1821 matching path names that contain "/", to avoid LTS (leaning
1822 toothpick syndrome). If "?" is the delimiter, then a match-
1823 only-once rule applies, described in "m?PATTERN?" below. If
1824 "'" (single quote) is the delimiter, no variable interpolation
1825 is performed on the PATTERN. When using a delimiter character
1826 valid in an identifier, whitespace is required after the "m".
1827
1828 PATTERN may contain variables, which will be interpolated every
1829 time the pattern search is evaluated, except for when the
1830 delimiter is a single quote. (Note that $(, $), and $| are not
1831 interpolated because they look like end-of-string tests.) Perl
1832 will not recompile the pattern unless an interpolated variable
1833 that it contains changes. You can force Perl to skip the test
1834 and never recompile by adding a "/o" (which stands for "once")
1835 after the trailing delimiter. Once upon a time, Perl would
1836 recompile regular expressions unnecessarily, and this modifier
1837 was useful to tell it not to do so, in the interests of speed.
1838 But now, the only reasons to use "/o" are one of:
1839
1840 1. The variables are thousands of characters long and you know
1841 that they don't change, and you need to wring out the last
1842 little bit of speed by having Perl skip testing for that.
1843 (There is a maintenance penalty for doing this, as
1844 mentioning "/o" constitutes a promise that you won't change
1845 the variables in the pattern. If you do change them, Perl
1846 won't even notice.)
1847
1848 2. you want the pattern to use the initial values of the
1849 variables regardless of whether they change or not. (But
1850 there are saner ways of accomplishing this than using
1851 "/o".)
1852
1853 3. If the pattern contains embedded code, such as
1854
1855 use re 'eval';
1856 $code = 'foo(?{ $x })';
1857 /$code/
1858
1859 then perl will recompile each time, even though the pattern
1860 string hasn't changed, to ensure that the current value of
1861 $x is seen each time. Use "/o" if you want to avoid this.
1862
1863 The bottom line is that using "/o" is almost never a good idea.
1864
1865 The empty pattern "//"
1866 If the PATTERN evaluates to the empty string, the last
1867 successfully matched regular expression is used instead. In
1868 this case, only the "g" and "c" flags on the empty pattern are
1869 honored; the other flags are taken from the original pattern.
1870 If no match has previously succeeded, this will (silently) act
1871 instead as a genuine empty pattern (which will always match).
1872
1873 Note that it's possible to confuse Perl into thinking "//" (the
1874 empty regex) is really "//" (the defined-or operator). Perl is
1875 usually pretty good about this, but some pathological cases
1876 might trigger this, such as "$x///" (is that "($x) / (//)" or
1877 "$x // /"?) and "print $fh //" ("print $fh(//" or
1878 "print($fh //"?). In all of these examples, Perl will assume
1879 you meant defined-or. If you meant the empty regex, just use
1880 parentheses or spaces to disambiguate, or even prefix the empty
1881 regex with an "m" (so "//" becomes "m//").
1882
1883 Matching in list context
1884 If the "/g" option is not used, "m//" in list context returns a
1885 list consisting of the subexpressions matched by the
1886 parentheses in the pattern, that is, ($1, $2, $3...) (Note
1887 that here $1 etc. are also set). When there are no parentheses
1888 in the pattern, the return value is the list "(1)" for success.
1889 With or without parentheses, an empty list is returned upon
1890 failure.
1891
1892 Examples:
1893
1894 open(TTY, "+</dev/tty")
1895 || die "can't access /dev/tty: $!";
1896
1897 <TTY> =~ /^y/i && foo(); # do foo if desired
1898
1899 if (/Version: *([0-9.]*)/) { $version = $1; }
1900
1901 next if m#^/usr/spool/uucp#;
1902
1903 # poor man's grep
1904 $arg = shift;
1905 while (<>) {
1906 print if /$arg/o; # compile only once (no longer needed!)
1907 }
1908
1909 if (($F1, $F2, $Etc) = ($foo =~ /^(\S+)\s+(\S+)\s*(.*)/))
1910
1911 This last example splits $foo into the first two words and the
1912 remainder of the line, and assigns those three fields to $F1,
1913 $F2, and $Etc. The conditional is true if any variables were
1914 assigned; that is, if the pattern matched.
1915
1916 The "/g" modifier specifies global pattern matching--that is,
1917 matching as many times as possible within the string. How it
1918 behaves depends on the context. In list context, it returns a
1919 list of the substrings matched by any capturing parentheses in
1920 the regular expression. If there are no parentheses, it
1921 returns a list of all the matched strings, as if there were
1922 parentheses around the whole pattern.
1923
1924 In scalar context, each execution of "m//g" finds the next
1925 match, returning true if it matches, and false if there is no
1926 further match. The position after the last match can be read
1927 or set using the "pos()" function; see "pos" in perlfunc. A
1928 failed match normally resets the search position to the
1929 beginning of the string, but you can avoid that by adding the
1930 "/c" modifier (for example, "m//gc"). Modifying the target
1931 string also resets the search position.
1932
1933 "\G assertion"
1934 You can intermix "m//g" matches with "m/\G.../g", where "\G" is
1935 a zero-width assertion that matches the exact position where
1936 the previous "m//g", if any, left off. Without the "/g"
1937 modifier, the "\G" assertion still anchors at "pos()" as it was
1938 at the start of the operation (see "pos" in perlfunc), but the
1939 match is of course only attempted once. Using "\G" without
1940 "/g" on a target string that has not previously had a "/g"
1941 match applied to it is the same as using the "\A" assertion to
1942 match the beginning of the string. Note also that, currently,
1943 "\G" is only properly supported when anchored at the very
1944 beginning of the pattern.
1945
1946 Examples:
1947
1948 # list context
1949 ($one,$five,$fifteen) = (`uptime` =~ /(\d+\.\d+)/g);
1950
1951 # scalar context
1952 local $/ = "";
1953 while ($paragraph = <>) {
1954 while ($paragraph =~ /\p{Ll}['")]*[.!?]+['")]*\s/g) {
1955 $sentences++;
1956 }
1957 }
1958 say $sentences;
1959
1960 Here's another way to check for sentences in a paragraph:
1961
1962 my $sentence_rx = qr{
1963 (?: (?<= ^ ) | (?<= \s ) ) # after start-of-string or
1964 # whitespace
1965 \p{Lu} # capital letter
1966 .*? # a bunch of anything
1967 (?<= \S ) # that ends in non-
1968 # whitespace
1969 (?<! \b [DMS]r ) # but isn't a common abbr.
1970 (?<! \b Mrs )
1971 (?<! \b Sra )
1972 (?<! \b St )
1973 [.?!] # followed by a sentence
1974 # ender
1975 (?= $ | \s ) # in front of end-of-string
1976 # or whitespace
1977 }sx;
1978 local $/ = "";
1979 while (my $paragraph = <>) {
1980 say "NEW PARAGRAPH";
1981 my $count = 0;
1982 while ($paragraph =~ /($sentence_rx)/g) {
1983 printf "\tgot sentence %d: <%s>\n", ++$count, $1;
1984 }
1985 }
1986
1987 Here's how to use "m//gc" with "\G":
1988
1989 $_ = "ppooqppqq";
1990 while ($i++ < 2) {
1991 print "1: '";
1992 print $1 while /(o)/gc; print "', pos=", pos, "\n";
1993 print "2: '";
1994 print $1 if /\G(q)/gc; print "', pos=", pos, "\n";
1995 print "3: '";
1996 print $1 while /(p)/gc; print "', pos=", pos, "\n";
1997 }
1998 print "Final: '$1', pos=",pos,"\n" if /\G(.)/;
1999
2000 The last example should print:
2001
2002 1: 'oo', pos=4
2003 2: 'q', pos=5
2004 3: 'pp', pos=7
2005 1: '', pos=7
2006 2: 'q', pos=8
2007 3: '', pos=8
2008 Final: 'q', pos=8
2009
2010 Notice that the final match matched "q" instead of "p", which a
2011 match without the "\G" anchor would have done. Also note that
2012 the final match did not update "pos". "pos" is only updated on
2013 a "/g" match. If the final match did indeed match "p", it's a
2014 good bet that you're running an ancient (pre-5.6.0) version of
2015 Perl.
2016
2017 A useful idiom for "lex"-like scanners is "/\G.../gc". You can
2018 combine several regexps like this to process a string part-by-
2019 part, doing different actions depending on which regexp
2020 matched. Each regexp tries to match where the previous one
2021 leaves off.
2022
2023 $_ = <<'EOL';
2024 $url = URI::URL->new( "http://example.com/" );
2025 die if $url eq "xXx";
2026 EOL
2027
2028 LOOP: {
2029 print(" digits"), redo LOOP if /\G\d+\b[,.;]?\s*/gc;
2030 print(" lowercase"), redo LOOP
2031 if /\G\p{Ll}+\b[,.;]?\s*/gc;
2032 print(" UPPERCASE"), redo LOOP
2033 if /\G\p{Lu}+\b[,.;]?\s*/gc;
2034 print(" Capitalized"), redo LOOP
2035 if /\G\p{Lu}\p{Ll}+\b[,.;]?\s*/gc;
2036 print(" MiXeD"), redo LOOP if /\G\pL+\b[,.;]?\s*/gc;
2037 print(" alphanumeric"), redo LOOP
2038 if /\G[\p{Alpha}\pN]+\b[,.;]?\s*/gc;
2039 print(" line-noise"), redo LOOP if /\G\W+/gc;
2040 print ". That's all!\n";
2041 }
2042
2043 Here is the output (split into several lines):
2044
2045 line-noise lowercase line-noise UPPERCASE line-noise UPPERCASE
2046 line-noise lowercase line-noise lowercase line-noise lowercase
2047 lowercase line-noise lowercase lowercase line-noise lowercase
2048 lowercase line-noise MiXeD line-noise. That's all!
2049
2050 "m?PATTERN?msixpodualngc"
2051 This is just like the "m/PATTERN/" search, except that it
2052 matches only once between calls to the "reset()" operator.
2053 This is a useful optimization when you want to see only the
2054 first occurrence of something in each file of a set of files,
2055 for instance. Only "m??" patterns local to the current
2056 package are reset.
2057
2058 while (<>) {
2059 if (m?^$?) {
2060 # blank line between header and body
2061 }
2062 } continue {
2063 reset if eof; # clear m?? status for next file
2064 }
2065
2066 Another example switched the first "latin1" encoding it finds
2067 to "utf8" in a pod file:
2068
2069 s//utf8/ if m? ^ =encoding \h+ \K latin1 ?x;
2070
2071 The match-once behavior is controlled by the match delimiter
2072 being "?"; with any other delimiter this is the normal "m//"
2073 operator.
2074
2075 In the past, the leading "m" in "m?PATTERN?" was optional, but
2076 omitting it would produce a deprecation warning. As of
2077 v5.22.0, omitting it produces a syntax error. If you encounter
2078 this construct in older code, you can just add "m".
2079
2080 "s/PATTERN/REPLACEMENT/msixpodualngcer"
2081 Searches a string for a pattern, and if found, replaces that
2082 pattern with the replacement text and returns the number of
2083 substitutions made. Otherwise it returns false (a value that
2084 is both an empty string ("") and numeric zero (0) as described
2085 in "Relational Operators").
2086
2087 If the "/r" (non-destructive) option is used then it runs the
2088 substitution on a copy of the string and instead of returning
2089 the number of substitutions, it returns the copy whether or not
2090 a substitution occurred. The original string is never changed
2091 when "/r" is used. The copy will always be a plain string,
2092 even if the input is an object or a tied variable.
2093
2094 If no string is specified via the "=~" or "!~" operator, the $_
2095 variable is searched and modified. Unless the "/r" option is
2096 used, the string specified must be a scalar variable, an array
2097 element, a hash element, or an assignment to one of those; that
2098 is, some sort of scalar lvalue.
2099
2100 If the delimiter chosen is a single quote, no variable
2101 interpolation is done on either the PATTERN or the REPLACEMENT.
2102 Otherwise, if the PATTERN contains a "$" that looks like a
2103 variable rather than an end-of-string test, the variable will
2104 be interpolated into the pattern at run-time. If you want the
2105 pattern compiled only once the first time the variable is
2106 interpolated, use the "/o" option. If the pattern evaluates to
2107 the empty string, the last successfully executed regular
2108 expression is used instead. See perlre for further explanation
2109 on these.
2110
2111 Options are as with "m//" with the addition of the following
2112 replacement specific options:
2113
2114 e Evaluate the right side as an expression.
2115 ee Evaluate the right side as a string then eval the
2116 result.
2117 r Return substitution and leave the original string
2118 untouched.
2119
2120 Any non-whitespace delimiter may replace the slashes. Add
2121 space after the "s" when using a character allowed in
2122 identifiers. If single quotes are used, no interpretation is
2123 done on the replacement string (the "/e" modifier overrides
2124 this, however). Note that Perl treats backticks as normal
2125 delimiters; the replacement text is not evaluated as a command.
2126 If the PATTERN is delimited by bracketing quotes, the
2127 REPLACEMENT has its own pair of quotes, which may or may not be
2128 bracketing quotes, for example, "s(foo)(bar)" or "s<foo>/bar/".
2129 A "/e" will cause the replacement portion to be treated as a
2130 full-fledged Perl expression and evaluated right then and
2131 there. It is, however, syntax checked at compile-time. A
2132 second "e" modifier will cause the replacement portion to be
2133 "eval"ed before being run as a Perl expression.
2134
2135 Examples:
2136
2137 s/\bgreen\b/mauve/g; # don't change wintergreen
2138
2139 $path =~ s|/usr/bin|/usr/local/bin|;
2140
2141 s/Login: $foo/Login: $bar/; # run-time pattern
2142
2143 ($foo = $bar) =~ s/this/that/; # copy first, then
2144 # change
2145 ($foo = "$bar") =~ s/this/that/; # convert to string,
2146 # copy, then change
2147 $foo = $bar =~ s/this/that/r; # Same as above using /r
2148 $foo = $bar =~ s/this/that/r
2149 =~ s/that/the other/r; # Chained substitutes
2150 # using /r
2151 @foo = map { s/this/that/r } @bar # /r is very useful in
2152 # maps
2153
2154 $count = ($paragraph =~ s/Mister\b/Mr./g); # get change-cnt
2155
2156 $_ = 'abc123xyz';
2157 s/\d+/$&*2/e; # yields 'abc246xyz'
2158 s/\d+/sprintf("%5d",$&)/e; # yields 'abc 246xyz'
2159 s/\w/$& x 2/eg; # yields 'aabbcc 224466xxyyzz'
2160
2161 s/%(.)/$percent{$1}/g; # change percent escapes; no /e
2162 s/%(.)/$percent{$1} || $&/ge; # expr now, so /e
2163 s/^=(\w+)/pod($1)/ge; # use function call
2164
2165 $_ = 'abc123xyz';
2166 $x = s/abc/def/r; # $x is 'def123xyz' and
2167 # $_ remains 'abc123xyz'.
2168
2169 # expand variables in $_, but dynamics only, using
2170 # symbolic dereferencing
2171 s/\$(\w+)/${$1}/g;
2172
2173 # Add one to the value of any numbers in the string
2174 s/(\d+)/1 + $1/eg;
2175
2176 # Titlecase words in the last 30 characters only
2177 substr($str, -30) =~ s/\b(\p{Alpha}+)\b/\u\L$1/g;
2178
2179 # This will expand any embedded scalar variable
2180 # (including lexicals) in $_ : First $1 is interpolated
2181 # to the variable name, and then evaluated
2182 s/(\$\w+)/$1/eeg;
2183
2184 # Delete (most) C comments.
2185 $program =~ s {
2186 /\* # Match the opening delimiter.
2187 .*? # Match a minimal number of characters.
2188 \*/ # Match the closing delimiter.
2189 } []gsx;
2190
2191 s/^\s*(.*?)\s*$/$1/; # trim whitespace in $_,
2192 # expensively
2193
2194 for ($variable) { # trim whitespace in $variable,
2195 # cheap
2196 s/^\s+//;
2197 s/\s+$//;
2198 }
2199
2200 s/([^ ]*) *([^ ]*)/$2 $1/; # reverse 1st two fields
2201
2202 $foo !~ s/A/a/g; # Lowercase all A's in $foo; return
2203 # 0 if any were found and changed;
2204 # otherwise return 1
2205
2206 Note the use of "$" instead of "\" in the last example. Unlike
2207 sed, we use the \<digit> form only in the left hand side.
2208 Anywhere else it's $<digit>.
2209
2210 Occasionally, you can't use just a "/g" to get all the changes
2211 to occur that you might want. Here are two common cases:
2212
2213 # put commas in the right places in an integer
2214 1 while s/(\d)(\d\d\d)(?!\d)/$1,$2/g;
2215
2216 # expand tabs to 8-column spacing
2217 1 while s/\t+/' ' x (length($&)*8 - length($`)%8)/e;
2218
2219 While "s///" accepts the "/c" flag, it has no effect beyond
2220 producing a warning if warnings are enabled.
2221
2222 Quote-Like Operators
2223 "q/STRING/"
2224 'STRING'
2225 A single-quoted, literal string. A backslash represents a
2226 backslash unless followed by the delimiter or another backslash, in
2227 which case the delimiter or backslash is interpolated.
2228
2229 $foo = q!I said, "You said, 'She said it.'"!;
2230 $bar = q('This is it.');
2231 $baz = '\n'; # a two-character string
2232
2233 "qq/STRING/"
2234 "STRING"
2235 A double-quoted, interpolated string.
2236
2237 $_ .= qq
2238 (*** The previous line contains the naughty word "$1".\n)
2239 if /\b(tcl|java|python)\b/i; # :-)
2240 $baz = "\n"; # a one-character string
2241
2242 "qx/STRING/"
2243 "`STRING`"
2244 A string which is (possibly) interpolated and then executed as a
2245 system command, via /bin/sh or its equivalent if required. Shell
2246 wildcards, pipes, and redirections will be honored. Similarly to
2247 "system", if the string contains no shell metacharacters then it
2248 will executed directly. The collected standard output of the
2249 command is returned; standard error is unaffected. In scalar
2250 context, it comes back as a single (potentially multi-line) string,
2251 or "undef" if the shell (or command) could not be started. In list
2252 context, returns a list of lines (however you've defined lines with
2253 $/ or $INPUT_RECORD_SEPARATOR), or an empty list if the shell (or
2254 command) could not be started.
2255
2256 Because backticks do not affect standard error, use shell file
2257 descriptor syntax (assuming the shell supports this) if you care to
2258 address this. To capture a command's STDERR and STDOUT together:
2259
2260 $output = `cmd 2>&1`;
2261
2262 To capture a command's STDOUT but discard its STDERR:
2263
2264 $output = `cmd 2>/dev/null`;
2265
2266 To capture a command's STDERR but discard its STDOUT (ordering is
2267 important here):
2268
2269 $output = `cmd 2>&1 1>/dev/null`;
2270
2271 To exchange a command's STDOUT and STDERR in order to capture the
2272 STDERR but leave its STDOUT to come out the old STDERR:
2273
2274 $output = `cmd 3>&1 1>&2 2>&3 3>&-`;
2275
2276 To read both a command's STDOUT and its STDERR separately, it's
2277 easiest to redirect them separately to files, and then read from
2278 those files when the program is done:
2279
2280 system("program args 1>program.stdout 2>program.stderr");
2281
2282 The STDIN filehandle used by the command is inherited from Perl's
2283 STDIN. For example:
2284
2285 open(SPLAT, "stuff") || die "can't open stuff: $!";
2286 open(STDIN, "<&SPLAT") || die "can't dupe SPLAT: $!";
2287 print STDOUT `sort`;
2288
2289 will print the sorted contents of the file named "stuff".
2290
2291 Using single-quote as a delimiter protects the command from Perl's
2292 double-quote interpolation, passing it on to the shell instead:
2293
2294 $perl_info = qx(ps $$); # that's Perl's $$
2295 $shell_info = qx'ps $$'; # that's the new shell's $$
2296
2297 How that string gets evaluated is entirely subject to the command
2298 interpreter on your system. On most platforms, you will have to
2299 protect shell metacharacters if you want them treated literally.
2300 This is in practice difficult to do, as it's unclear how to escape
2301 which characters. See perlsec for a clean and safe example of a
2302 manual "fork()" and "exec()" to emulate backticks safely.
2303
2304 On some platforms (notably DOS-like ones), the shell may not be
2305 capable of dealing with multiline commands, so putting newlines in
2306 the string may not get you what you want. You may be able to
2307 evaluate multiple commands in a single line by separating them with
2308 the command separator character, if your shell supports that (for
2309 example, ";" on many Unix shells and "&" on the Windows NT "cmd"
2310 shell).
2311
2312 Perl will attempt to flush all files opened for output before
2313 starting the child process, but this may not be supported on some
2314 platforms (see perlport). To be safe, you may need to set $|
2315 ($AUTOFLUSH in "English") or call the "autoflush()" method of
2316 "IO::Handle" on any open handles.
2317
2318 Beware that some command shells may place restrictions on the
2319 length of the command line. You must ensure your strings don't
2320 exceed this limit after any necessary interpolations. See the
2321 platform-specific release notes for more details about your
2322 particular environment.
2323
2324 Using this operator can lead to programs that are difficult to
2325 port, because the shell commands called vary between systems, and
2326 may in fact not be present at all. As one example, the "type"
2327 command under the POSIX shell is very different from the "type"
2328 command under DOS. That doesn't mean you should go out of your way
2329 to avoid backticks when they're the right way to get something
2330 done. Perl was made to be a glue language, and one of the things
2331 it glues together is commands. Just understand what you're getting
2332 yourself into.
2333
2334 Like "system", backticks put the child process exit code in $?. If
2335 you'd like to manually inspect failure, you can check all possible
2336 failure modes by inspecting $? like this:
2337
2338 if ($? == -1) {
2339 print "failed to execute: $!\n";
2340 }
2341 elsif ($? & 127) {
2342 printf "child died with signal %d, %s coredump\n",
2343 ($? & 127), ($? & 128) ? 'with' : 'without';
2344 }
2345 else {
2346 printf "child exited with value %d\n", $? >> 8;
2347 }
2348
2349 Use the open pragma to control the I/O layers used when reading the
2350 output of the command, for example:
2351
2352 use open IN => ":encoding(UTF-8)";
2353 my $x = `cmd-producing-utf-8`;
2354
2355 "qx//" can also be called like a function with "readpipe" in
2356 perlfunc.
2357
2358 See "I/O Operators" for more discussion.
2359
2360 "qw/STRING/"
2361 Evaluates to a list of the words extracted out of STRING, using
2362 embedded whitespace as the word delimiters. It can be understood
2363 as being roughly equivalent to:
2364
2365 split(" ", q/STRING/);
2366
2367 the differences being that it only splits on ASCII whitespace,
2368 generates a real list at compile time, and in scalar context it
2369 returns the last element in the list. So this expression:
2370
2371 qw(foo bar baz)
2372
2373 is semantically equivalent to the list:
2374
2375 "foo", "bar", "baz"
2376
2377 Some frequently seen examples:
2378
2379 use POSIX qw( setlocale localeconv )
2380 @EXPORT = qw( foo bar baz );
2381
2382 A common mistake is to try to separate the words with commas or to
2383 put comments into a multi-line "qw"-string. For this reason, the
2384 "use warnings" pragma and the -w switch (that is, the $^W variable)
2385 produces warnings if the STRING contains the "," or the "#"
2386 character.
2387
2388 "tr/SEARCHLIST/REPLACEMENTLIST/cdsr"
2389 "y/SEARCHLIST/REPLACEMENTLIST/cdsr"
2390 Transliterates all occurrences of the characters found (or not
2391 found if the "/c" modifier is specified) in the search list with
2392 the positionally corresponding character in the replacement list,
2393 possibly deleting some, depending on the modifiers specified. It
2394 returns the number of characters replaced or deleted. If no string
2395 is specified via the "=~" or "!~" operator, the $_ string is
2396 transliterated.
2397
2398 For sed devotees, "y" is provided as a synonym for "tr".
2399
2400 If the "/r" (non-destructive) option is present, a new copy of the
2401 string is made and its characters transliterated, and this copy is
2402 returned no matter whether it was modified or not: the original
2403 string is always left unchanged. The new copy is always a plain
2404 string, even if the input string is an object or a tied variable.
2405
2406 Unless the "/r" option is used, the string specified with "=~" must
2407 be a scalar variable, an array element, a hash element, or an
2408 assignment to one of those; in other words, an lvalue.
2409
2410 The characters delimitting SEARCHLIST and REPLACEMENTLIST can be
2411 any printable character, not just forward slashes. If they are
2412 single quotes ("tr'SEARCHLIST'REPLACEMENTLIST'"), the only
2413 interpolation is removal of "\" from pairs of "\\".
2414
2415 Otherwise, a character range may be specified with a hyphen, so
2416 "tr/A-J/0-9/" does the same replacement as
2417 "tr/ACEGIBDFHJ/0246813579/".
2418
2419 If the SEARCHLIST is delimited by bracketing quotes, the
2420 REPLACEMENTLIST must have its own pair of quotes, which may or may
2421 not be bracketing quotes; for example, "tr[aeiouy][yuoiea]" or
2422 "tr(+\-*/)/ABCD/".
2423
2424 Characters may be literals, or (if the delimiters aren't single
2425 quotes) any of the escape sequences accepted in double-quoted
2426 strings. But there is never any variable interpolation, so "$" and
2427 "@" are always treated as literals. A hyphen at the beginning or
2428 end, or preceded by a backslash is also always considered a
2429 literal. Escape sequence details are in the table near the
2430 beginning of this section.
2431
2432 Note that "tr" does not do regular expression character classes
2433 such as "\d" or "\pL". The "tr" operator is not equivalent to the
2434 tr(1) utility. "tr[a-z][A-Z]" will uppercase the 26 letters "a"
2435 through "z", but for case changing not confined to ASCII, use "lc",
2436 "uc", "lcfirst", "ucfirst" (all documented in perlfunc), or the
2437 substitution operator "s/PATTERN/REPLACEMENT/" (with "\U", "\u",
2438 "\L", and "\l" string-interpolation escapes in the REPLACEMENT
2439 portion).
2440
2441 Most ranges are unportable between character sets, but certain ones
2442 signal Perl to do special handling to make them portable. There
2443 are two classes of portable ranges. The first are any subsets of
2444 the ranges "A-Z", "a-z", and "0-9", when expressed as literal
2445 characters.
2446
2447 tr/h-k/H-K/
2448
2449 capitalizes the letters "h", "i", "j", and "k" and nothing else, no
2450 matter what the platform's character set is. In contrast, all of
2451
2452 tr/\x68-\x6B/\x48-\x4B/
2453 tr/h-\x6B/H-\x4B/
2454 tr/\x68-k/\x48-K/
2455
2456 do the same capitalizations as the previous example when run on
2457 ASCII platforms, but something completely different on EBCDIC ones.
2458
2459 The second class of portable ranges is invoked when one or both of
2460 the range's end points are expressed as "\N{...}"
2461
2462 $string =~ tr/\N{U+20}-\N{U+7E}//d;
2463
2464 removes from $string all the platform's characters which are
2465 equivalent to any of Unicode U+0020, U+0021, ... U+007D, U+007E.
2466 This is a portable range, and has the same effect on every platform
2467 it is run on. In this example, these are the ASCII printable
2468 characters. So after this is run, $string has only controls and
2469 characters which have no ASCII equivalents.
2470
2471 But, even for portable ranges, it is not generally obvious what is
2472 included without having to look things up in the manual. A sound
2473 principle is to use only ranges that both begin from, and end at,
2474 either ASCII alphabetics of equal case ("b-e", "B-E"), or digits
2475 ("1-4"). Anything else is unclear (and unportable unless "\N{...}"
2476 is used). If in doubt, spell out the character sets in full.
2477
2478 Options:
2479
2480 c Complement the SEARCHLIST.
2481 d Delete found but unreplaced characters.
2482 r Return the modified string and leave the original string
2483 untouched.
2484 s Squash duplicate replaced characters.
2485
2486 If the "/d" modifier is specified, any characters specified by
2487 SEARCHLIST not found in REPLACEMENTLIST are deleted. (Note that
2488 this is slightly more flexible than the behavior of some tr
2489 programs, which delete anything they find in the SEARCHLIST,
2490 period.)
2491
2492 If the "/s" modifier is specified, sequences of characters, all in
2493 a row, that were transliterated to the same character are squashed
2494 down to a single instance of that character.
2495
2496 my $a = "aaabbbca";
2497 $a =~ tr/ab/dd/s; # $a now is "dcd"
2498
2499 If the "/d" modifier is used, the REPLACEMENTLIST is always
2500 interpreted exactly as specified. Otherwise, if the
2501 REPLACEMENTLIST is shorter than the SEARCHLIST, the final
2502 character, if any, is replicated until it is long enough. There
2503 won't be a final character if and only if the REPLACEMENTLIST is
2504 empty, in which case REPLACEMENTLIST is copied from SEARCHLIST.
2505 An empty REPLACEMENTLIST is useful for counting characters in a
2506 class, or for squashing character sequences in a class.
2507
2508 tr/abcd// tr/abcd/abcd/
2509 tr/abcd/AB/ tr/abcd/ABBB/
2510 tr/abcd//d s/[abcd]//g
2511 tr/abcd/AB/d (tr/ab/AB/ + s/[cd]//g) - but run together
2512
2513 If the "/c" modifier is specified, the characters to be
2514 transliterated are the ones NOT in SEARCHLIST, that is, it is
2515 complemented. If "/d" and/or "/s" are also specified, they apply
2516 to the complemented SEARCHLIST. Recall, that if REPLACEMENTLIST is
2517 empty (except under "/d") a copy of SEARCHLIST is used instead.
2518 That copy is made after complementing under "/c". SEARCHLIST is
2519 sorted by code point order after complementing, and any
2520 REPLACEMENTLIST is applied to that sorted result. This means that
2521 under "/c", the order of the characters specified in SEARCHLIST is
2522 irrelevant. This can lead to different results on EBCDIC systems
2523 if REPLACEMENTLIST contains more than one character, hence it is
2524 generally non-portable to use "/c" with such a REPLACEMENTLIST.
2525
2526 Another way of describing the operation is this: If "/c" is
2527 specified, the SEARCHLIST is sorted by code point order, then
2528 complemented. If REPLACEMENTLIST is empty and "/d" is not
2529 specified, REPLACEMENTLIST is replaced by a copy of SEARCHLIST (as
2530 modified under "/c"), and these potentially modified lists are used
2531 as the basis for what follows. Any character in the target string
2532 that isn't in SEARCHLIST is passed through unchanged. Every other
2533 character in the target string is replaced by the character in
2534 REPLACEMENTLIST that positionally corresponds to its mate in
2535 SEARCHLIST, except that under "/s", the 2nd and following
2536 characters are squeezed out in a sequence of characters in a row
2537 that all translate to the same character. If SEARCHLIST is longer
2538 than REPLACEMENTLIST, characters in the target string that match a
2539 character in SEARCHLIST that doesn't have a correspondence in
2540 REPLACEMENTLIST are either deleted from the target string if "/d"
2541 is specified; or replaced by the final character in REPLACEMENTLIST
2542 if "/d" isn't specified.
2543
2544 Some examples:
2545
2546 $ARGV[1] =~ tr/A-Z/a-z/; # canonicalize to lower case ASCII
2547
2548 $cnt = tr/*/*/; # count the stars in $_
2549 $cnt = tr/*//; # same thing
2550
2551 $cnt = $sky =~ tr/*/*/; # count the stars in $sky
2552 $cnt = $sky =~ tr/*//; # same thing
2553
2554 $cnt = $sky =~ tr/*//c; # count all the non-stars in $sky
2555 $cnt = $sky =~ tr/*/*/c; # same, but transliterate each non-star
2556 # into a star, leaving the already-stars
2557 # alone. Afterwards, everything in $sky
2558 # is a star.
2559
2560 $cnt = tr/0-9//; # count the ASCII digits in $_
2561
2562 tr/a-zA-Z//s; # bookkeeper -> bokeper
2563 tr/o/o/s; # bookkeeper -> bokkeeper
2564 tr/oe/oe/s; # bookkeeper -> bokkeper
2565 tr/oe//s; # bookkeeper -> bokkeper
2566 tr/oe/o/s; # bookkeeper -> bokkopor
2567
2568 ($HOST = $host) =~ tr/a-z/A-Z/;
2569 $HOST = $host =~ tr/a-z/A-Z/r; # same thing
2570
2571 $HOST = $host =~ tr/a-z/A-Z/r # chained with s///r
2572 =~ s/:/ -p/r;
2573
2574 tr/a-zA-Z/ /cs; # change non-alphas to single space
2575
2576 @stripped = map tr/a-zA-Z/ /csr, @original;
2577 # /r with map
2578
2579 tr [\200-\377]
2580 [\000-\177]; # wickedly delete 8th bit
2581
2582 $foo !~ tr/A/a/ # transliterate all the A's in $foo to 'a',
2583 # return 0 if any were found and changed.
2584 # Otherwise return 1
2585
2586 If multiple transliterations are given for a character, only the
2587 first one is used:
2588
2589 tr/AAA/XYZ/
2590
2591 will transliterate any A to X.
2592
2593 Because the transliteration table is built at compile time, neither
2594 the SEARCHLIST nor the REPLACEMENTLIST are subjected to double
2595 quote interpolation. That means that if you want to use variables,
2596 you must use an "eval()":
2597
2598 eval "tr/$oldlist/$newlist/";
2599 die $@ if $@;
2600
2601 eval "tr/$oldlist/$newlist/, 1" or die $@;
2602
2603 "<<EOF"
2604 A line-oriented form of quoting is based on the shell "here-
2605 document" syntax. Following a "<<" you specify a string to
2606 terminate the quoted material, and all lines following the current
2607 line down to the terminating string are the value of the item.
2608
2609 Prefixing the terminating string with a "~" specifies that you want
2610 to use "Indented Here-docs" (see below).
2611
2612 The terminating string may be either an identifier (a word), or
2613 some quoted text. An unquoted identifier works like double quotes.
2614 There may not be a space between the "<<" and the identifier,
2615 unless the identifier is explicitly quoted. The terminating string
2616 must appear by itself (unquoted and with no surrounding whitespace)
2617 on the terminating line.
2618
2619 If the terminating string is quoted, the type of quotes used
2620 determine the treatment of the text.
2621
2622 Double Quotes
2623 Double quotes indicate that the text will be interpolated using
2624 exactly the same rules as normal double quoted strings.
2625
2626 print <<EOF;
2627 The price is $Price.
2628 EOF
2629
2630 print << "EOF"; # same as above
2631 The price is $Price.
2632 EOF
2633
2634 Single Quotes
2635 Single quotes indicate the text is to be treated literally with
2636 no interpolation of its content. This is similar to single
2637 quoted strings except that backslashes have no special meaning,
2638 with "\\" being treated as two backslashes and not one as they
2639 would in every other quoting construct.
2640
2641 Just as in the shell, a backslashed bareword following the "<<"
2642 means the same thing as a single-quoted string does:
2643
2644 $cost = <<'VISTA'; # hasta la ...
2645 That'll be $10 please, ma'am.
2646 VISTA
2647
2648 $cost = <<\VISTA; # Same thing!
2649 That'll be $10 please, ma'am.
2650 VISTA
2651
2652 This is the only form of quoting in perl where there is no need
2653 to worry about escaping content, something that code generators
2654 can and do make good use of.
2655
2656 Backticks
2657 The content of the here doc is treated just as it would be if
2658 the string were embedded in backticks. Thus the content is
2659 interpolated as though it were double quoted and then executed
2660 via the shell, with the results of the execution returned.
2661
2662 print << `EOC`; # execute command and get results
2663 echo hi there
2664 EOC
2665
2666 Indented Here-docs
2667 The here-doc modifier "~" allows you to indent your here-docs
2668 to make the code more readable:
2669
2670 if ($some_var) {
2671 print <<~EOF;
2672 This is a here-doc
2673 EOF
2674 }
2675
2676 This will print...
2677
2678 This is a here-doc
2679
2680 ...with no leading whitespace.
2681
2682 The delimiter is used to determine the exact whitespace to
2683 remove from the beginning of each line. All lines must have at
2684 least the same starting whitespace (except lines only
2685 containing a newline) or perl will croak. Tabs and spaces can
2686 be mixed, but are matched exactly. One tab will not be equal
2687 to 8 spaces!
2688
2689 Additional beginning whitespace (beyond what preceded the
2690 delimiter) will be preserved:
2691
2692 print <<~EOF;
2693 This text is not indented
2694 This text is indented with two spaces
2695 This text is indented with two tabs
2696 EOF
2697
2698 Finally, the modifier may be used with all of the forms
2699 mentioned above:
2700
2701 <<~\EOF;
2702 <<~'EOF'
2703 <<~"EOF"
2704 <<~`EOF`
2705
2706 And whitespace may be used between the "~" and quoted
2707 delimiters:
2708
2709 <<~ 'EOF'; # ... "EOF", `EOF`
2710
2711 It is possible to stack multiple here-docs in a row:
2712
2713 print <<"foo", <<"bar"; # you can stack them
2714 I said foo.
2715 foo
2716 I said bar.
2717 bar
2718
2719 myfunc(<< "THIS", 23, <<'THAT');
2720 Here's a line
2721 or two.
2722 THIS
2723 and here's another.
2724 THAT
2725
2726 Just don't forget that you have to put a semicolon on the end to
2727 finish the statement, as Perl doesn't know you're not going to try
2728 to do this:
2729
2730 print <<ABC
2731 179231
2732 ABC
2733 + 20;
2734
2735 If you want to remove the line terminator from your here-docs, use
2736 "chomp()".
2737
2738 chomp($string = <<'END');
2739 This is a string.
2740 END
2741
2742 If you want your here-docs to be indented with the rest of the
2743 code, use the "<<~FOO" construct described under "Indented Here-
2744 docs":
2745
2746 $quote = <<~'FINIS';
2747 The Road goes ever on and on,
2748 down from the door where it began.
2749 FINIS
2750
2751 If you use a here-doc within a delimited construct, such as in
2752 "s///eg", the quoted material must still come on the line following
2753 the "<<FOO" marker, which means it may be inside the delimited
2754 construct:
2755
2756 s/this/<<E . 'that'
2757 the other
2758 E
2759 . 'more '/eg;
2760
2761 It works this way as of Perl 5.18. Historically, it was
2762 inconsistent, and you would have to write
2763
2764 s/this/<<E . 'that'
2765 . 'more '/eg;
2766 the other
2767 E
2768
2769 outside of string evals.
2770
2771 Additionally, quoting rules for the end-of-string identifier are
2772 unrelated to Perl's quoting rules. "q()", "qq()", and the like are
2773 not supported in place of '' and "", and the only interpolation is
2774 for backslashing the quoting character:
2775
2776 print << "abc\"def";
2777 testing...
2778 abc"def
2779
2780 Finally, quoted strings cannot span multiple lines. The general
2781 rule is that the identifier must be a string literal. Stick with
2782 that, and you should be safe.
2783
2784 Gory details of parsing quoted constructs
2785 When presented with something that might have several different
2786 interpretations, Perl uses the DWIM (that's "Do What I Mean") principle
2787 to pick the most probable interpretation. This strategy is so
2788 successful that Perl programmers often do not suspect the ambivalence
2789 of what they write. But from time to time, Perl's notions differ
2790 substantially from what the author honestly meant.
2791
2792 This section hopes to clarify how Perl handles quoted constructs.
2793 Although the most common reason to learn this is to unravel
2794 labyrinthine regular expressions, because the initial steps of parsing
2795 are the same for all quoting operators, they are all discussed
2796 together.
2797
2798 The most important Perl parsing rule is the first one discussed below:
2799 when processing a quoted construct, Perl first finds the end of that
2800 construct, then interprets its contents. If you understand this rule,
2801 you may skip the rest of this section on the first reading. The other
2802 rules are likely to contradict the user's expectations much less
2803 frequently than this first one.
2804
2805 Some passes discussed below are performed concurrently, but because
2806 their results are the same, we consider them individually. For
2807 different quoting constructs, Perl performs different numbers of
2808 passes, from one to four, but these passes are always performed in the
2809 same order.
2810
2811 Finding the end
2812 The first pass is finding the end of the quoted construct. This
2813 results in saving to a safe location a copy of the text (between
2814 the starting and ending delimiters), normalized as necessary to
2815 avoid needing to know what the original delimiters were.
2816
2817 If the construct is a here-doc, the ending delimiter is a line that
2818 has a terminating string as the content. Therefore "<<EOF" is
2819 terminated by "EOF" immediately followed by "\n" and starting from
2820 the first column of the terminating line. When searching for the
2821 terminating line of a here-doc, nothing is skipped. In other
2822 words, lines after the here-doc syntax are compared with the
2823 terminating string line by line.
2824
2825 For the constructs except here-docs, single characters are used as
2826 starting and ending delimiters. If the starting delimiter is an
2827 opening punctuation (that is "(", "[", "{", or "<"), the ending
2828 delimiter is the corresponding closing punctuation (that is ")",
2829 "]", "}", or ">"). If the starting delimiter is an unpaired
2830 character like "/" or a closing punctuation, the ending delimiter
2831 is the same as the starting delimiter. Therefore a "/" terminates
2832 a "qq//" construct, while a "]" terminates both "qq[]" and "qq]]"
2833 constructs.
2834
2835 When searching for single-character delimiters, escaped delimiters
2836 and "\\" are skipped. For example, while searching for terminating
2837 "/", combinations of "\\" and "\/" are skipped. If the delimiters
2838 are bracketing, nested pairs are also skipped. For example, while
2839 searching for a closing "]" paired with the opening "[",
2840 combinations of "\\", "\]", and "\[" are all skipped, and nested
2841 "[" and "]" are skipped as well. However, when backslashes are
2842 used as the delimiters (like "qq\\" and "tr\\\"), nothing is
2843 skipped. During the search for the end, backslashes that escape
2844 delimiters or other backslashes are removed (exactly speaking, they
2845 are not copied to the safe location).
2846
2847 For constructs with three-part delimiters ("s///", "y///", and
2848 "tr///"), the search is repeated once more. If the first delimiter
2849 is not an opening punctuation, the three delimiters must be the
2850 same, such as "s!!!" and "tr)))", in which case the second
2851 delimiter terminates the left part and starts the right part at
2852 once. If the left part is delimited by bracketing punctuation
2853 (that is "()", "[]", "{}", or "<>"), the right part needs another
2854 pair of delimiters such as "s(){}" and "tr[]//". In these cases,
2855 whitespace and comments are allowed between the two parts, although
2856 the comment must follow at least one whitespace character;
2857 otherwise a character expected as the start of the comment may be
2858 regarded as the starting delimiter of the right part.
2859
2860 During this search no attention is paid to the semantics of the
2861 construct. Thus:
2862
2863 "$hash{"$foo/$bar"}"
2864
2865 or:
2866
2867 m/
2868 bar # NOT a comment, this slash / terminated m//!
2869 /x
2870
2871 do not form legal quoted expressions. The quoted part ends on the
2872 first """ and "/", and the rest happens to be a syntax error.
2873 Because the slash that terminated "m//" was followed by a "SPACE",
2874 the example above is not "m//x", but rather "m//" with no "/x"
2875 modifier. So the embedded "#" is interpreted as a literal "#".
2876
2877 Also no attention is paid to "\c\" (multichar control char syntax)
2878 during this search. Thus the second "\" in "qq/\c\/" is
2879 interpreted as a part of "\/", and the following "/" is not
2880 recognized as a delimiter. Instead, use "\034" or "\x1c" at the
2881 end of quoted constructs.
2882
2883 Interpolation
2884 The next step is interpolation in the text obtained, which is now
2885 delimiter-independent. There are multiple cases.
2886
2887 "<<'EOF'"
2888 No interpolation is performed. Note that the combination "\\"
2889 is left intact, since escaped delimiters are not available for
2890 here-docs.
2891
2892 "m''", the pattern of "s'''"
2893 No interpolation is performed at this stage. Any backslashed
2894 sequences including "\\" are treated at the stage to "parsing
2895 regular expressions".
2896
2897 '', "q//", "tr'''", "y'''", the replacement of "s'''"
2898 The only interpolation is removal of "\" from pairs of "\\".
2899 Therefore "-" in "tr'''" and "y'''" is treated literally as a
2900 hyphen and no character range is available. "\1" in the
2901 replacement of "s'''" does not work as $1.
2902
2903 "tr///", "y///"
2904 No variable interpolation occurs. String modifying
2905 combinations for case and quoting such as "\Q", "\U", and "\E"
2906 are not recognized. The other escape sequences such as "\200"
2907 and "\t" and backslashed characters such as "\\" and "\-" are
2908 converted to appropriate literals. The character "-" is
2909 treated specially and therefore "\-" is treated as a literal
2910 "-".
2911
2912 "", "``", "qq//", "qx//", "<file*glob>", "<<"EOF""
2913 "\Q", "\U", "\u", "\L", "\l", "\F" (possibly paired with "\E")
2914 are converted to corresponding Perl constructs. Thus,
2915 "$foo\Qbaz$bar" is converted to
2916 "$foo . (quotemeta("baz" . $bar))" internally. The other
2917 escape sequences such as "\200" and "\t" and backslashed
2918 characters such as "\\" and "\-" are replaced with appropriate
2919 expansions.
2920
2921 Let it be stressed that whatever falls between "\Q" and "\E" is
2922 interpolated in the usual way. Something like "\Q\\E" has no
2923 "\E" inside. Instead, it has "\Q", "\\", and "E", so the
2924 result is the same as for "\\\\E". As a general rule,
2925 backslashes between "\Q" and "\E" may lead to counterintuitive
2926 results. So, "\Q\t\E" is converted to "quotemeta("\t")", which
2927 is the same as "\\\t" (since TAB is not alphanumeric). Note
2928 also that:
2929
2930 $str = '\t';
2931 return "\Q$str";
2932
2933 may be closer to the conjectural intention of the writer of
2934 "\Q\t\E".
2935
2936 Interpolated scalars and arrays are converted internally to the
2937 "join" and "." catenation operations. Thus, "$foo XXX '@arr'"
2938 becomes:
2939
2940 $foo . " XXX '" . (join $", @arr) . "'";
2941
2942 All operations above are performed simultaneously, left to
2943 right.
2944
2945 Because the result of "\Q STRING \E" has all metacharacters
2946 quoted, there is no way to insert a literal "$" or "@" inside a
2947 "\Q\E" pair. If protected by "\", "$" will be quoted to become
2948 "\\\$"; if not, it is interpreted as the start of an
2949 interpolated scalar.
2950
2951 Note also that the interpolation code needs to make a decision
2952 on where the interpolated scalar ends. For instance, whether
2953 "a $x -> {c}" really means:
2954
2955 "a " . $x . " -> {c}";
2956
2957 or:
2958
2959 "a " . $x -> {c};
2960
2961 Most of the time, the longest possible text that does not
2962 include spaces between components and which contains matching
2963 braces or brackets. because the outcome may be determined by
2964 voting based on heuristic estimators, the result is not
2965 strictly predictable. Fortunately, it's usually correct for
2966 ambiguous cases.
2967
2968 the replacement of "s///"
2969 Processing of "\Q", "\U", "\u", "\L", "\l", "\F" and
2970 interpolation happens as with "qq//" constructs.
2971
2972 It is at this step that "\1" is begrudgingly converted to $1 in
2973 the replacement text of "s///", in order to correct the
2974 incorrigible sed hackers who haven't picked up the saner idiom
2975 yet. A warning is emitted if the "use warnings" pragma or the
2976 -w command-line flag (that is, the $^W variable) was set.
2977
2978 "RE" in "m?RE?", "/RE/", "m/RE/", "s/RE/foo/",
2979 Processing of "\Q", "\U", "\u", "\L", "\l", "\F", "\E", and
2980 interpolation happens (almost) as with "qq//" constructs.
2981
2982 Processing of "\N{...}" is also done here, and compiled into an
2983 intermediate form for the regex compiler. (This is because, as
2984 mentioned below, the regex compilation may be done at execution
2985 time, and "\N{...}" is a compile-time construct.)
2986
2987 However any other combinations of "\" followed by a character
2988 are not substituted but only skipped, in order to parse them as
2989 regular expressions at the following step. As "\c" is skipped
2990 at this step, "@" of "\c@" in RE is possibly treated as an
2991 array symbol (for example @foo), even though the same text in
2992 "qq//" gives interpolation of "\c@".
2993
2994 Code blocks such as "(?{BLOCK})" are handled by temporarily
2995 passing control back to the perl parser, in a similar way that
2996 an interpolated array subscript expression such as
2997 "foo$array[1+f("[xyz")]bar" would be.
2998
2999 Moreover, inside "(?{BLOCK})", "(?# comment )", and a
3000 "#"-comment in a "/x"-regular expression, no processing is
3001 performed whatsoever. This is the first step at which the
3002 presence of the "/x" modifier is relevant.
3003
3004 Interpolation in patterns has several quirks: $|, $(, $), "@+"
3005 and "@-" are not interpolated, and constructs $var[SOMETHING]
3006 are voted (by several different estimators) to be either an
3007 array element or $var followed by an RE alternative. This is
3008 where the notation "${arr[$bar]}" comes handy: "/${arr[0-9]}/"
3009 is interpreted as array element "-9", not as a regular
3010 expression from the variable $arr followed by a digit, which
3011 would be the interpretation of "/$arr[0-9]/". Since voting
3012 among different estimators may occur, the result is not
3013 predictable.
3014
3015 The lack of processing of "\\" creates specific restrictions on
3016 the post-processed text. If the delimiter is "/", one cannot
3017 get the combination "\/" into the result of this step. "/"
3018 will finish the regular expression, "\/" will be stripped to
3019 "/" on the previous step, and "\\/" will be left as is.
3020 Because "/" is equivalent to "\/" inside a regular expression,
3021 this does not matter unless the delimiter happens to be
3022 character special to the RE engine, such as in "s*foo*bar*",
3023 "m[foo]", or "m?foo?"; or an alphanumeric char, as in:
3024
3025 m m ^ a \s* b mmx;
3026
3027 In the RE above, which is intentionally obfuscated for
3028 illustration, the delimiter is "m", the modifier is "mx", and
3029 after delimiter-removal the RE is the same as for
3030 "m/ ^ a \s* b /mx". There's more than one reason you're
3031 encouraged to restrict your delimiters to non-alphanumeric,
3032 non-whitespace choices.
3033
3034 This step is the last one for all constructs except regular
3035 expressions, which are processed further.
3036
3037 parsing regular expressions
3038 Previous steps were performed during the compilation of Perl code,
3039 but this one happens at run time, although it may be optimized to
3040 be calculated at compile time if appropriate. After preprocessing
3041 described above, and possibly after evaluation if concatenation,
3042 joining, casing translation, or metaquoting are involved, the
3043 resulting string is passed to the RE engine for compilation.
3044
3045 Whatever happens in the RE engine might be better discussed in
3046 perlre, but for the sake of continuity, we shall do so here.
3047
3048 This is another step where the presence of the "/x" modifier is
3049 relevant. The RE engine scans the string from left to right and
3050 converts it into a finite automaton.
3051
3052 Backslashed characters are either replaced with corresponding
3053 literal strings (as with "\{"), or else they generate special nodes
3054 in the finite automaton (as with "\b"). Characters special to the
3055 RE engine (such as "|") generate corresponding nodes or groups of
3056 nodes. "(?#...)" comments are ignored. All the rest is either
3057 converted to literal strings to match, or else is ignored (as is
3058 whitespace and "#"-style comments if "/x" is present).
3059
3060 Parsing of the bracketed character class construct, "[...]", is
3061 rather different than the rule used for the rest of the pattern.
3062 The terminator of this construct is found using the same rules as
3063 for finding the terminator of a "{}"-delimited construct, the only
3064 exception being that "]" immediately following "[" is treated as
3065 though preceded by a backslash.
3066
3067 The terminator of runtime "(?{...})" is found by temporarily
3068 switching control to the perl parser, which should stop at the
3069 point where the logically balancing terminating "}" is found.
3070
3071 It is possible to inspect both the string given to RE engine and
3072 the resulting finite automaton. See the arguments
3073 "debug"/"debugcolor" in the "use re" pragma, as well as Perl's -Dr
3074 command-line switch documented in "Command Switches" in perlrun.
3075
3076 Optimization of regular expressions
3077 This step is listed for completeness only. Since it does not
3078 change semantics, details of this step are not documented and are
3079 subject to change without notice. This step is performed over the
3080 finite automaton that was generated during the previous pass.
3081
3082 It is at this stage that "split()" silently optimizes "/^/" to mean
3083 "/^/m".
3084
3085 I/O Operators
3086 There are several I/O operators you should know about.
3087
3088 A string enclosed by backticks (grave accents) first undergoes double-
3089 quote interpolation. It is then interpreted as an external command,
3090 and the output of that command is the value of the backtick string,
3091 like in a shell. In scalar context, a single string consisting of all
3092 output is returned. In list context, a list of values is returned, one
3093 per line of output. (You can set $/ to use a different line
3094 terminator.) The command is executed each time the pseudo-literal is
3095 evaluated. The status value of the command is returned in $? (see
3096 perlvar for the interpretation of $?). Unlike in csh, no translation
3097 is done on the return data--newlines remain newlines. Unlike in any of
3098 the shells, single quotes do not hide variable names in the command
3099 from interpretation. To pass a literal dollar-sign through to the
3100 shell you need to hide it with a backslash. The generalized form of
3101 backticks is "qx//", or you can call the "readpipe" in perlfunc
3102 function. (Because backticks always undergo shell expansion as well,
3103 see perlsec for security concerns.)
3104
3105 In scalar context, evaluating a filehandle in angle brackets yields the
3106 next line from that file (the newline, if any, included), or "undef" at
3107 end-of-file or on error. When $/ is set to "undef" (sometimes known as
3108 file-slurp mode) and the file is empty, it returns '' the first time,
3109 followed by "undef" subsequently.
3110
3111 Ordinarily you must assign the returned value to a variable, but there
3112 is one situation where an automatic assignment happens. If and only if
3113 the input symbol is the only thing inside the conditional of a "while"
3114 statement (even if disguised as a "for(;;)" loop), the value is
3115 automatically assigned to the global variable $_, destroying whatever
3116 was there previously. (This may seem like an odd thing to you, but
3117 you'll use the construct in almost every Perl script you write.) The
3118 $_ variable is not implicitly localized. You'll have to put a
3119 "local $_;" before the loop if you want that to happen. Furthermore,
3120 if the input symbol or an explicit assignment of the input symbol to a
3121 scalar is used as a "while"/"for" condition, then the condition
3122 actually tests for definedness of the expression's value, not for its
3123 regular truth value.
3124
3125 Thus the following lines are equivalent:
3126
3127 while (defined($_ = <STDIN>)) { print; }
3128 while ($_ = <STDIN>) { print; }
3129 while (<STDIN>) { print; }
3130 for (;<STDIN>;) { print; }
3131 print while defined($_ = <STDIN>);
3132 print while ($_ = <STDIN>);
3133 print while <STDIN>;
3134
3135 This also behaves similarly, but assigns to a lexical variable instead
3136 of to $_:
3137
3138 while (my $line = <STDIN>) { print $line }
3139
3140 In these loop constructs, the assigned value (whether assignment is
3141 automatic or explicit) is then tested to see whether it is defined.
3142 The defined test avoids problems where the line has a string value that
3143 would be treated as false by Perl; for example a "" or a "0" with no
3144 trailing newline. If you really mean for such values to terminate the
3145 loop, they should be tested for explicitly:
3146
3147 while (($_ = <STDIN>) ne '0') { ... }
3148 while (<STDIN>) { last unless $_; ... }
3149
3150 In other boolean contexts, "<FILEHANDLE>" without an explicit "defined"
3151 test or comparison elicits a warning if the "use warnings" pragma or
3152 the -w command-line switch (the $^W variable) is in effect.
3153
3154 The filehandles STDIN, STDOUT, and STDERR are predefined. (The
3155 filehandles "stdin", "stdout", and "stderr" will also work except in
3156 packages, where they would be interpreted as local identifiers rather
3157 than global.) Additional filehandles may be created with the "open()"
3158 function, amongst others. See perlopentut and "open" in perlfunc for
3159 details on this.
3160
3161 If a "<FILEHANDLE>" is used in a context that is looking for a list, a
3162 list comprising all input lines is returned, one line per list element.
3163 It's easy to grow to a rather large data space this way, so use with
3164 care.
3165
3166 "<FILEHANDLE>" may also be spelled "readline(*FILEHANDLE)". See
3167 "readline" in perlfunc.
3168
3169 The null filehandle "<>" (sometimes called the diamond operator) is
3170 special: it can be used to emulate the behavior of sed and awk, and any
3171 other Unix filter program that takes a list of filenames, doing the
3172 same to each line of input from all of them. Input from "<>" comes
3173 either from standard input, or from each file listed on the command
3174 line. Here's how it works: the first time "<>" is evaluated, the @ARGV
3175 array is checked, and if it is empty, $ARGV[0] is set to "-", which
3176 when opened gives you standard input. The @ARGV array is then
3177 processed as a list of filenames. The loop
3178
3179 while (<>) {
3180 ... # code for each line
3181 }
3182
3183 is equivalent to the following Perl-like pseudo code:
3184
3185 unshift(@ARGV, '-') unless @ARGV;
3186 while ($ARGV = shift) {
3187 open(ARGV, $ARGV);
3188 while (<ARGV>) {
3189 ... # code for each line
3190 }
3191 }
3192
3193 except that it isn't so cumbersome to say, and will actually work. It
3194 really does shift the @ARGV array and put the current filename into the
3195 $ARGV variable. It also uses filehandle ARGV internally. "<>" is just
3196 a synonym for "<ARGV>", which is magical. (The pseudo code above
3197 doesn't work because it treats "<ARGV>" as non-magical.)
3198
3199 Since the null filehandle uses the two argument form of "open" in
3200 perlfunc it interprets special characters, so if you have a script like
3201 this:
3202
3203 while (<>) {
3204 print;
3205 }
3206
3207 and call it with "perl dangerous.pl 'rm -rfv *|'", it actually opens a
3208 pipe, executes the "rm" command and reads "rm"'s output from that pipe.
3209 If you want all items in @ARGV to be interpreted as file names, you can
3210 use the module "ARGV::readonly" from CPAN, or use the double diamond
3211 bracket:
3212
3213 while (<<>>) {
3214 print;
3215 }
3216
3217 Using double angle brackets inside of a while causes the open to use
3218 the three argument form (with the second argument being "<"), so all
3219 arguments in "ARGV" are treated as literal filenames (including "-").
3220 (Note that for convenience, if you use "<<>>" and if @ARGV is empty, it
3221 will still read from the standard input.)
3222
3223 You can modify @ARGV before the first "<>" as long as the array ends up
3224 containing the list of filenames you really want. Line numbers ($.)
3225 continue as though the input were one big happy file. See the example
3226 in "eof" in perlfunc for how to reset line numbers on each file.
3227
3228 If you want to set @ARGV to your own list of files, go right ahead.
3229 This sets @ARGV to all plain text files if no @ARGV was given:
3230
3231 @ARGV = grep { -f && -T } glob('*') unless @ARGV;
3232
3233 You can even set them to pipe commands. For example, this
3234 automatically filters compressed arguments through gzip:
3235
3236 @ARGV = map { /\.(gz|Z)$/ ? "gzip -dc < $_ |" : $_ } @ARGV;
3237
3238 If you want to pass switches into your script, you can use one of the
3239 "Getopts" modules or put a loop on the front like this:
3240
3241 while ($_ = $ARGV[0], /^-/) {
3242 shift;
3243 last if /^--$/;
3244 if (/^-D(.*)/) { $debug = $1 }
3245 if (/^-v/) { $verbose++ }
3246 # ... # other switches
3247 }
3248
3249 while (<>) {
3250 # ... # code for each line
3251 }
3252
3253 The "<>" symbol will return "undef" for end-of-file only once. If you
3254 call it again after this, it will assume you are processing another
3255 @ARGV list, and if you haven't set @ARGV, will read input from STDIN.
3256
3257 If what the angle brackets contain is a simple scalar variable (for
3258 example, $foo), then that variable contains the name of the filehandle
3259 to input from, or its typeglob, or a reference to the same. For
3260 example:
3261
3262 $fh = \*STDIN;
3263 $line = <$fh>;
3264
3265 If what's within the angle brackets is neither a filehandle nor a
3266 simple scalar variable containing a filehandle name, typeglob, or
3267 typeglob reference, it is interpreted as a filename pattern to be
3268 globbed, and either a list of filenames or the next filename in the
3269 list is returned, depending on context. This distinction is determined
3270 on syntactic grounds alone. That means "<$x>" is always a "readline()"
3271 from an indirect handle, but "<$hash{key}>" is always a "glob()".
3272 That's because $x is a simple scalar variable, but $hash{key} is
3273 not--it's a hash element. Even "<$x >" (note the extra space) is
3274 treated as "glob("$x ")", not "readline($x)".
3275
3276 One level of double-quote interpretation is done first, but you can't
3277 say "<$foo>" because that's an indirect filehandle as explained in the
3278 previous paragraph. (In older versions of Perl, programmers would
3279 insert curly brackets to force interpretation as a filename glob:
3280 "<${foo}>". These days, it's considered cleaner to call the internal
3281 function directly as "glob($foo)", which is probably the right way to
3282 have done it in the first place.) For example:
3283
3284 while (<*.c>) {
3285 chmod 0644, $_;
3286 }
3287
3288 is roughly equivalent to:
3289
3290 open(FOO, "echo *.c | tr -s ' \t\r\f' '\\012\\012\\012\\012'|");
3291 while (<FOO>) {
3292 chomp;
3293 chmod 0644, $_;
3294 }
3295
3296 except that the globbing is actually done internally using the standard
3297 "File::Glob" extension. Of course, the shortest way to do the above
3298 is:
3299
3300 chmod 0644, <*.c>;
3301
3302 A (file)glob evaluates its (embedded) argument only when it is starting
3303 a new list. All values must be read before it will start over. In
3304 list context, this isn't important because you automatically get them
3305 all anyway. However, in scalar context the operator returns the next
3306 value each time it's called, or "undef" when the list has run out. As
3307 with filehandle reads, an automatic "defined" is generated when the
3308 glob occurs in the test part of a "while", because legal glob returns
3309 (for example, a file called 0) would otherwise terminate the loop.
3310 Again, "undef" is returned only once. So if you're expecting a single
3311 value from a glob, it is much better to say
3312
3313 ($file) = <blurch*>;
3314
3315 than
3316
3317 $file = <blurch*>;
3318
3319 because the latter will alternate between returning a filename and
3320 returning false.
3321
3322 If you're trying to do variable interpolation, it's definitely better
3323 to use the "glob()" function, because the older notation can cause
3324 people to become confused with the indirect filehandle notation.
3325
3326 @files = glob("$dir/*.[ch]");
3327 @files = glob($files[$i]);
3328
3329 If an angle-bracket-based globbing expression is used as the condition
3330 of a "while" or "for" loop, then it will be implicitly assigned to $_.
3331 If either a globbing expression or an explicit assignment of a globbing
3332 expression to a scalar is used as a "while"/"for" condition, then the
3333 condition actually tests for definedness of the expression's value, not
3334 for its regular truth value.
3335
3336 Constant Folding
3337 Like C, Perl does a certain amount of expression evaluation at compile
3338 time whenever it determines that all arguments to an operator are
3339 static and have no side effects. In particular, string concatenation
3340 happens at compile time between literals that don't do variable
3341 substitution. Backslash interpolation also happens at compile time.
3342 You can say
3343
3344 'Now is the time for all'
3345 . "\n"
3346 . 'good men to come to.'
3347
3348 and this all reduces to one string internally. Likewise, if you say
3349
3350 foreach $file (@filenames) {
3351 if (-s $file > 5 + 100 * 2**16) { }
3352 }
3353
3354 the compiler precomputes the number which that expression represents so
3355 that the interpreter won't have to.
3356
3357 No-ops
3358 Perl doesn't officially have a no-op operator, but the bare constants 0
3359 and 1 are special-cased not to produce a warning in void context, so
3360 you can for example safely do
3361
3362 1 while foo();
3363
3364 Bitwise String Operators
3365 Bitstrings of any size may be manipulated by the bitwise operators ("~
3366 | & ^").
3367
3368 If the operands to a binary bitwise op are strings of different sizes,
3369 | and ^ ops act as though the shorter operand had additional zero bits
3370 on the right, while the & op acts as though the longer operand were
3371 truncated to the length of the shorter. The granularity for such
3372 extension or truncation is one or more bytes.
3373
3374 # ASCII-based examples
3375 print "j p \n" ^ " a h"; # prints "JAPH\n"
3376 print "JA" | " ph\n"; # prints "japh\n"
3377 print "japh\nJunk" & '_____'; # prints "JAPH\n";
3378 print 'p N$' ^ " E<H\n"; # prints "Perl\n";
3379
3380 If you are intending to manipulate bitstrings, be certain that you're
3381 supplying bitstrings: If an operand is a number, that will imply a
3382 numeric bitwise operation. You may explicitly show which type of
3383 operation you intend by using "" or "0+", as in the examples below.
3384
3385 $foo = 150 | 105; # yields 255 (0x96 | 0x69 is 0xFF)
3386 $foo = '150' | 105; # yields 255
3387 $foo = 150 | '105'; # yields 255
3388 $foo = '150' | '105'; # yields string '155' (under ASCII)
3389
3390 $baz = 0+$foo & 0+$bar; # both ops explicitly numeric
3391 $biz = "$foo" ^ "$bar"; # both ops explicitly stringy
3392
3393 This somewhat unpredictable behavior can be avoided with the "bitwise"
3394 feature, new in Perl 5.22. You can enable it via
3395 "use feature 'bitwise'" or "use v5.28". Before Perl 5.28, it used to
3396 emit a warning in the "experimental::bitwise" category. Under this
3397 feature, the four standard bitwise operators ("~ | & ^") are always
3398 numeric. Adding a dot after each operator ("~. |. &. ^.") forces it to
3399 treat its operands as strings:
3400
3401 use feature "bitwise";
3402 $foo = 150 | 105; # yields 255 (0x96 | 0x69 is 0xFF)
3403 $foo = '150' | 105; # yields 255
3404 $foo = 150 | '105'; # yields 255
3405 $foo = '150' | '105'; # yields 255
3406 $foo = 150 |. 105; # yields string '155'
3407 $foo = '150' |. 105; # yields string '155'
3408 $foo = 150 |.'105'; # yields string '155'
3409 $foo = '150' |.'105'; # yields string '155'
3410
3411 $baz = $foo & $bar; # both operands numeric
3412 $biz = $foo ^. $bar; # both operands stringy
3413
3414 The assignment variants of these operators ("&= |= ^= &.= |.= ^.=")
3415 behave likewise under the feature.
3416
3417 It is a fatal error if an operand contains a character whose ordinal
3418 value is above 0xFF, and hence not expressible except in UTF-8. The
3419 operation is performed on a non-UTF-8 copy for other operands encoded
3420 in UTF-8. See "Byte and Character Semantics" in perlunicode.
3421
3422 See "vec" in perlfunc for information on how to manipulate individual
3423 bits in a bit vector.
3424
3425 Integer Arithmetic
3426 By default, Perl assumes that it must do most of its arithmetic in
3427 floating point. But by saying
3428
3429 use integer;
3430
3431 you may tell the compiler to use integer operations (see integer for a
3432 detailed explanation) from here to the end of the enclosing BLOCK. An
3433 inner BLOCK may countermand this by saying
3434
3435 no integer;
3436
3437 which lasts until the end of that BLOCK. Note that this doesn't mean
3438 everything is an integer, merely that Perl will use integer operations
3439 for arithmetic, comparison, and bitwise operators. For example, even
3440 under "use integer", if you take the sqrt(2), you'll still get
3441 1.4142135623731 or so.
3442
3443 Used on numbers, the bitwise operators ("&" "|" "^" "~" "<<" ">>")
3444 always produce integral results. (But see also "Bitwise String
3445 Operators".) However, "use integer" still has meaning for them. By
3446 default, their results are interpreted as unsigned integers, but if
3447 "use integer" is in effect, their results are interpreted as signed
3448 integers. For example, "~0" usually evaluates to a large integral
3449 value. However, "use integer; ~0" is "-1" on two's-complement
3450 machines.
3451
3452 Floating-point Arithmetic
3453 While "use integer" provides integer-only arithmetic, there is no
3454 analogous mechanism to provide automatic rounding or truncation to a
3455 certain number of decimal places. For rounding to a certain number of
3456 digits, "sprintf()" or "printf()" is usually the easiest route. See
3457 perlfaq4.
3458
3459 Floating-point numbers are only approximations to what a mathematician
3460 would call real numbers. There are infinitely more reals than floats,
3461 so some corners must be cut. For example:
3462
3463 printf "%.20g\n", 123456789123456789;
3464 # produces 123456789123456784
3465
3466 Testing for exact floating-point equality or inequality is not a good
3467 idea. Here's a (relatively expensive) work-around to compare whether
3468 two floating-point numbers are equal to a particular number of decimal
3469 places. See Knuth, volume II, for a more robust treatment of this
3470 topic.
3471
3472 sub fp_equal {
3473 my ($X, $Y, $POINTS) = @_;
3474 my ($tX, $tY);
3475 $tX = sprintf("%.${POINTS}g", $X);
3476 $tY = sprintf("%.${POINTS}g", $Y);
3477 return $tX eq $tY;
3478 }
3479
3480 The POSIX module (part of the standard perl distribution) implements
3481 "ceil()", "floor()", and other mathematical and trigonometric
3482 functions. The "Math::Complex" module (part of the standard perl
3483 distribution) defines mathematical functions that work on both the
3484 reals and the imaginary numbers. "Math::Complex" is not as efficient
3485 as POSIX, but POSIX can't work with complex numbers.
3486
3487 Rounding in financial applications can have serious implications, and
3488 the rounding method used should be specified precisely. In these
3489 cases, it probably pays not to trust whichever system rounding is being
3490 used by Perl, but to instead implement the rounding function you need
3491 yourself.
3492
3493 Bigger Numbers
3494 The standard "Math::BigInt", "Math::BigRat", and "Math::BigFloat"
3495 modules, along with the "bignum", "bigint", and "bigrat" pragmas,
3496 provide variable-precision arithmetic and overloaded operators,
3497 although they're currently pretty slow. At the cost of some space and
3498 considerable speed, they avoid the normal pitfalls associated with
3499 limited-precision representations.
3500
3501 use 5.010;
3502 use bigint; # easy interface to Math::BigInt
3503 $x = 123456789123456789;
3504 say $x * $x;
3505 +15241578780673678515622620750190521
3506
3507 Or with rationals:
3508
3509 use 5.010;
3510 use bigrat;
3511 $x = 3/22;
3512 $y = 4/6;
3513 say "x/y is ", $x/$y;
3514 say "x*y is ", $x*$y;
3515 x/y is 9/44
3516 x*y is 1/11
3517
3518 Several modules let you calculate with unlimited or fixed precision
3519 (bound only by memory and CPU time). There are also some non-standard
3520 modules that provide faster implementations via external C libraries.
3521
3522 Here is a short, but incomplete summary:
3523
3524 Math::String treat string sequences like numbers
3525 Math::FixedPrecision calculate with a fixed precision
3526 Math::Currency for currency calculations
3527 Bit::Vector manipulate bit vectors fast (uses C)
3528 Math::BigIntFast Bit::Vector wrapper for big numbers
3529 Math::Pari provides access to the Pari C library
3530 Math::Cephes uses the external Cephes C library (no
3531 big numbers)
3532 Math::Cephes::Fraction fractions via the Cephes library
3533 Math::GMP another one using an external C library
3534 Math::GMPz an alternative interface to libgmp's big ints
3535 Math::GMPq an interface to libgmp's fraction numbers
3536 Math::GMPf an interface to libgmp's floating point numbers
3537
3538 Choose wisely.
3539
3540
3541
3542perl v5.34.1 2022-03-15 PERLOP(1)