http://www.perlmonks.org?node_id=1057127


in reply to Iterator to parse multiline string with \\n terminator

If your file is not terribly huge and/or you have enough memory, this is an alternative solution:
use Modern::Perl; my $file; { local $/ = ''; $file = <DATA>; } $file =~ s/\\\n/ /gs; my @lines = split /\n/, $file; say for @lines; __DATA__ First line Second line (part1)\ Second line (first continuation)\ Second line (second continuation) Third line (part1)\ Third line (first continuation)\ Third line (second continuation) Fourth line (part1)\ Fourth line (first continuation)\ Fourth line (second continuation)
Output:
First line Second line (part1) Second line (first continuation) Second line (seco +nd continuation) Third line (part1) Third line (first continuation) Third line (second +continuation) Fourth line (part1) Fourth line (first continuation) Fourth line (seco +nd continuation)

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics

Replies are listed 'Best First'.
Re^2: Iterator to parse multiline string with \\n terminator
by three18ti (Monk) on Oct 06, 2013 at 09:21 UTC

    Thanks!

    At the moment I'm only parsing a file that's a few megs / few thousands lines, however, I have the potential to be parsing a lot larger files so memory could become a problem in the future.