XML technologies / Regular expressions / Perl / Perl concepts for regular expressions / Expression context
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
The term context is very important in Perl, particularly for the match operator. An expression can essentially occur in three kinds of context: list context, scalar context, or void context. These names indicate the expected result type. List context expects a list of values; scalar context expects a single value. Both occur frequently and are most relevant to regular expressions here. Void context expects no value at all.
In the two assignments
$s = Ausdruck eins;
@a = Ausdruck zwei;, $s is a simple scalar variable: it contains a single value rather than a series. The first expression, however complex, is therefore in scalar context. Similarly, @a is an array variable. It demands a list of values, so the second expression is in list context.
The two expressions may be identical yet return completely different values and produce different side effects. Exactly what happens depends on the expression.
Consider the function localtime. In list context, it returns values corresponding to the current year, month, day, hour, and so on. In scalar context, it returns a string giving the same date in text form, such as ›Mon Jan 20 22:05:15 2003‹.
Or consider the read operator <DATEI>: in scalar context it reads and returns the next line of the file; in list context it reads all remaining lines and returns them as a list.
Many other Perl operations, like localtime and <DATEI>, adapt to context. The regex operators are no different: depending on context, the match operator returns only a Boolean match/no-match value or also a list of matched texts. Details appear later in this chapter.
Coercing an expression
Not every expression adapts to context on its own. Perl therefore has rules for what happens when a general expression occurs in an unintended context. Sometimes Perl simply coerces the expression to fit.
When a scalar occurs in list context, Perl simply turns it into a one-element list. Thus @a = 42 exakt dasselbe wie @a = (42).
The reverse does not work that way. There is no general rule for converting a list to a scalar. When a literal list is assigned to a scalar, as in
$var = ($das, &ist, 0xE, "i\n\e", 'Liste');, the comma operator returns the last element, here 'Liste'. When an array is assigned, as in $var = @array, the array’s length—its number of elements—is returned.
Other programming languages describe this adaptation with terms such as cast, type conversion or promotion . To me, these sound far too consistent (boring?) to describe Perl’s behavior adequately.
German edition copyright © 2008 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for private use.
Otherwise, this chapter from "Reguläre Ausdrücke" is subject to the same terms as the bound edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln