6.7. Reading Records with a Pattern SeparatorProblemYou want to read in records separated by a pattern, but Perl doesn't allow its input record separator variable to be a regular expression. Many problems, most obviously those involving the parsing of complex file formats, become a lot simpler when you are easily able to extract records that might be separated by a number of different strings. Discussion
Perl's record separator must be a fixed string, not a pattern. (After all,
awk
has to be better at
something
.) To sidestep this limitation, undefine the input record separator entirely so that the next line-read operation gets the rest of the file. This is sometimes called
slurp
mode, because it slurps in the whole file as one big string. Then
Here's an example, where the input stream is a text file that includes lines consisting of # .Ch, .Se and .Ss divide chunks of STDIN { local $/ = undef; @chunks = split(/^\.(Ch|Se|Ss)$/m, <>); } print "I read ", scalar(@chunks), " chunks.\n";
We create a localized version of
If you didn't want delimiters returned but still needed parentheses, you could use non-capturing parentheses in the pattern:
If you just want to split
before
a pattern but include the pattern in the return, use a look-ahead assertion: Be aware that this uses a lot of memory if the file is large. However, with today's machines and your typical text files, this is less often an issue now than it once was. Just don't try it on a 200-MB logfile unless you have plenty of virtual memory to use to swap out to disk with! Even if you do have enough swap space, you'll likely end up thrashing. See Also
The Copyright © 2002 O'Reilly & Associates. All rights reserved. |
|