如果段落之间有空格:按段落读取文本(设置$/ = "\n\n"'
),然后使用CPAN 中的Text::Autoformat。
示例(用常规文件句柄替换 DATA ——我只是在示例中使用它方便):
use strict;
use warnings;
use Text::Autoformat;
local $/ = "\n\n";
while (<DATA>) {
print autoformat $_, {left=>1, right=>80};
}
__DATA__
He watched Kahlan as she walked with her shoulders slumped
down.
He watched Kahlan as she walked with her shoulders slumped
down.
He watched Kahlan as she walked with her shoulders slumped
down.
He watched Kahlan as she walked with her shoulders slumped
down.
He watched Kahlan as she walked with her shoulders slumped
down.
He watched Kahlan as she walked with her shoulders slumped
down.
输出:
He watched Kahlan as she walked with her shoulders slumped down.
He watched Kahlan as she walked with her shoulders slumped down. He watched
Kahlan as she walked with her shoulders slumped down. He watched Kahlan as she
walked with her shoulders slumped down.
He watched Kahlan as she walked with her shoulders slumped down. He watched
Kahlan as she walked with her shoulders slumped down.