perl - 用于类似 Makefile 的续行的最干净的 Perl 解析器

标签 perl fileparsing redo

我正在编写的 perl 脚本需要解析具有延续行的文件,例如 Makefile。即以空格开头的行是前一行的一部分。

我写了下面的代码,但不觉得它很干净或 perl-ish(见鬼,它甚至没有使用“重做”!)

有很多边缘情况:奇数位置的 EOF、单行文件、以空行(或非空行或续行)开头或结尾的文件、空文件。我所有的测试用例(和代码)都在这里:http://whatexit.org/tal/flatten.tar

你能写出通过我所有测试的更简洁、perl-ish 的代码吗?

#!/usr/bin/perl -w

use strict;

sub process_file_with_continuations {
    my $processref = shift @_;
    my $nextline;
    my $line = <ARGV>;

    $line = '' unless defined $line;
    chomp $line;

    while (defined($nextline = <ARGV>)) {
        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
                &$processref($line . $nextline);
            }
            else {
                &$processref($line);
                &$processref($nextline) if $nextline ne '';
            }
            $line = '';
        }
        elsif ($nextline eq '') {  # blank line
            &$processref($line);
            $line = '';
        }
        elsif ($nextline =~ /^\s+/) {  # indented line
            $nextline =~ s/^\s+/ /;
            $line .= $nextline;
        }
        else {  # non-indented line
            &$processref($line) unless $line eq '';
            $line = $nextline;
        }
    }
    &$processref($line) unless $line eq '';
}

sub process_one_line {
    my $line = shift @_;
    print "$line\n";
}

process_file_with_continuations \&process_one_line;

最佳答案

如何将整个文件放入内存并使用正则表达式处理它。更多的'perlish'。这通过了您的测试,并且 更小更整洁:

#!/usr/bin/perl

use strict;
use warnings;

$/ = undef;             # we want no input record separator.
my $file = <>;          # slurp whole file

$file =~ s/^\n//;       # Remove newline at start of file
$file =~ s/\s+\n/\n/g;  # Remove trailing whitespace.
$file =~ s/\n\s*#[^\n]+//g;     # Remove comments.
$file =~ s/\n\s+/ /g;   # Merge continuations

# Done
print $file;

关于perl - 用于类似 Makefile 的续行的最干净的 Perl 解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1004696/

相关文章:

arrays - 在 Perl 中多次执行语句的简单方法

arrays - 为什么我的第一个哈希值在 Perl 中消失了?

c++ - 通过解析文件构建逻辑语句

python - 使用 pygame 和 lambda 实现撤消和重做

java - 如何在文本编辑器中设计撤销和重做?

C#:无法撤消插入的文本

ruby - 如何仅在某些 LaTeX 命令参数的范围内进行替换?

performance - 检查Perl中$ string是否以$ needle开头的最有效方法

python - 如何将具有换行符和制表符的python文件读入字符串?

powershell - 如何在 PowerShell 上将变量中的内容与哈希表进行比较