截断文件中的所有行,同时保留整个单词

truncate all lines in a file while preserving whole words

我正在尝试将文件的每一行缩短为 96 个字符,同时保留整个单词。如果一行小于或等于 96 个字符,我不想对该行执行任何操作。如果它超过 96 个字符,我希望它在保留整个单词的同时将其减少到小于 96 的最接近数量。当我 运行 此代码时,我得到一个空白文件。

use Text::Autoformat;

use strict;
use warnings;

#open the file
my $filename = $ARGV[0]; # store the 1st argument into the variable
open my $file, '<', $filename;
open my $fileout, '>>', $filename.96;

my @file = <$file>;  #each line of the file into an array

while (my $line = <$file>) {
  chomp $line;
  foreach (@file) {
#######
sub truncate($$) {
    my ( $line, $max ) = @_;

    # always do nothing if already short enough 
    ( length( $line ) <= $max ) and return $line;

    # forced to chop a word anyway
    if ( $line =~ /\s/ ) {
       return substr( $line, 0, $max );
    }
    # otherwise truncate on word boundary 
    $line =~ s/\S+$// and return $line;

    die; # unreachable
}
####### 

my $truncated  = &truncate($line,96);

print $fileout "$truncated\n";

  }
}       
close($file);
close($fileout);

你没有输出,因为你没有输入。

1. my @file = <$file>;  #each line of the file into an array
2. while (my $line = <$file>) { ...

<$file> 操作第 1 行在列表上下文 "consumes" 中将所有输入加载到 @file 中。第 2 行的 <$file> 操作没有更多的输入要读取,因此 while 循环不会执行。

您要么想要从文件句柄流式传输

# don't call @file = <$file>
while (my $line = <$file>) {
    chomp $line; 
    my $truncated = &truncate($line, 96);
    ...
}

或者从文件内容数组中读取

my @file = <$file>;
foreach my $line (@file) {
    chomp $line; 
    my $truncated = &truncate($line, 96);
    ...
}

如果输入量很大,前一种格式的优点是一次只将一行加载到内存中。