XML technologies / Regular expressions / Perl / The split operator / Special forms of the regex operand for split
(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)
Usually, the match operand of split is a regex literal or object, just as for the match operator. There are special cases, however:
An empty regex in
splitdoes not mean use the default regex. Instead, it tellssplitto split the input into individual characters. At the start of thesplitsection, we saw thatsplit(//, "Kleiner Test")produces a list of twelve elements:("K", "l", "e", ... "s", "t").A match operand that is a string (not a regex) consisting of one space behaves much like
/\s+/, except that leading whitespace is ignored. This imitates awk, which splits every input line this way. The behavior is useful elsewhere too.To make leading whitespace produce an empty element, use
m/\s+/. To retain trailing whitespace too, specify limit-1.Without a match operand, a one-space string, the preceding special case, is assumed. Thus
splitwithout operands is equivalent tosplit('●', $_, 0).Regex
˹^˼automatically enables modifier/m, enhanced line-anchor or multiline mode. For˹$˼, this inexplicably does not happen. I recommend specifyingm/^/mexplicitly for clarity.split(m/^/m, ...)easily splits a multiline string into separate lines.
split has no side effects
The match operand of split often resembles the match operator, but it is not one and has no side effects. The regex in split does not affect the default regex used by later matches or substitutions. Variables $&, $', $1 and so on are not set and remain untouched by split. Regarding side effects, split is isolated from the rest of the program. One exception concerns an obsolete and discouraged feature still implemented: when split is used in scalar or void context, its result elements are written to array @_, which also holds subroutine arguments. Avoid accidentally using split in scalar or void context. With warnings enabled by -w or use warnings , Perl warns about it.
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