Note that there are some explanatory texts on larger screens.

plurals
  1. POCleanest Perl parser for Makefile-like continuation lines
    primarykey
    data
    text
    <p>A perl script I'm writing needs to parse a file that has continuation lines like a Makefile. i.e. lines that begin with whitespace are part of the previous line.</p> <p>I wrote the code below but don't feel like it is very clean or perl-ish (heck, it doesn't even use "redo"!)</p> <p>There are many edge cases: EOF at odd places, single-line files, files that start or end with a blank line (or non-blank line, or continuation line), empty files. All my test cases (and code) are here: <a href="http://whatexit.org/tal/flatten.tar" rel="nofollow noreferrer">http://whatexit.org/tal/flatten.tar</a></p> <p>Can you write cleaner, perl-ish, code that passes all my tests?</p> <pre><code>#!/usr/bin/perl -w use strict; sub process_file_with_continuations { my $processref = shift @_; my $nextline; my $line = &lt;ARGV&gt;; $line = '' unless defined $line; chomp $line; while (defined($nextline = &lt;ARGV&gt;)) { chomp $nextline; next if $nextline =~ /^\s*#/; # skip comments $nextline =~ s/\s+$//g; # remove trailing whitespace if (eof()) { # Handle EOF $nextline =~ s/^\s+/ /; if ($nextline =~ /^\s+/) { # indented line &amp;$processref($line . $nextline); } else { &amp;$processref($line); &amp;$processref($nextline) if $nextline ne ''; } $line = ''; } elsif ($nextline eq '') { # blank line &amp;$processref($line); $line = ''; } elsif ($nextline =~ /^\s+/) { # indented line $nextline =~ s/^\s+/ /; $line .= $nextline; } else { # non-indented line &amp;$processref($line) unless $line eq ''; $line = $nextline; } } &amp;$processref($line) unless $line eq ''; } sub process_one_line { my $line = shift @_; print "$line\n"; } process_file_with_continuations \&amp;process_one_line; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload