Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Hello Monks,

I would like to parse a file that has single lines split into multiple lines by terminating the line with a "\".

I basically want to read a file line by line and grab all continued lines at once.

I was thinking about something along the lines of:

#/usr/bin/perl use strict; use warnings; use IO::File; use 5.010; my $filename = shift @ARGV; my $fh = IO::File->new($filename, 'r'); my $fh_iterator = sub { my $fh = shift; my $line = $fh->getline; } while (my $line = $fh_iterator->($fh)) { # do stuff to $line }

Where I get stuck is the logic to grab the next line if the line is terminated with a /\\\n/... My first thought was some kind of recursion:

$line .= $fh_iterator->($fh) if $line =~ /\\\n/

(Maybe I should be using a named sub instead of a closure; eventually I would like to include this as part of an object I'm trying to work out the parsing logic first)

I could just be over thinking the problem...

Thanks for your thoughts

Edit: Some further thinking I tried using a named sub instead of a closure

sub fh_iterator { my $fh = shift; my $line = $fh->getline; $line .= fh_iterator($fh) if $line =~ /\\\n/; return $line; }

But each time fh_iterator is called, it's going to clobber $line... So this doesn't do what I'd expect. I'd like to preserve the \ but should probably chomp the $fh->getline somehow.

In reply to Iterator to parse multiline string with \\n terminator by three18ti

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (5)
    As of 2020-02-27 00:12 GMT
    Find Nodes?
      Voting Booth?
      What numbers are you going to focus on primarily in 2020?

      Results (117 votes). Check out past polls.