Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://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

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2024-04-18 16:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found