in reply to Iterator to parse multiline string with \\n terminator
No need for recursion. Just change the anonymous sub as follows (untested):
my $fh_iterator = sub { my $fh = shift; my $line = $fh->getline(); $line .= $fh->getline() while $line =~ m{\\$}; return $line; }
Update: Here is a tested script which eliminates the Use of uninitialized value warning reported in the post below:
use strict; use warnings; use IO::File; my $filename = shift @ARGV; my $fh = IO::File->new($filename, 'r'); sub fh_iterator { my $fh = shift; my $line = $fh->getline(); if (defined $line) { $line .= $fh->getline() while $line =~ m{\\$}; } return $line; } while (my $line = fh_iterator($fh)) { print $line; }
Output:
16:29 >perl 738_SoPW.pl test.file foo \ bar \ baz single line 16:29 >
Hope that helps,
Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Iterator to parse multiline string with \\n terminator
by three18ti (Monk) on Oct 06, 2013 at 06:08 UTC | |
Re^2: Iterator to parse multiline string with \\n terminator
by three18ti (Monk) on Oct 06, 2013 at 09:20 UTC | |
by tinita (Parson) on Oct 06, 2013 at 12:46 UTC | |
by Athanasius (Archbishop) on Oct 06, 2013 at 16:28 UTC |
In Section
Seekers of Perl Wisdom