使用Text :: Wrap:wrap()减少行开头的空格

时间:2012-06-07 05:21:59

标签: perl

我想将列号限制为45个字符。我正在使用Text :: Wrap:wrap,如下所示。

$Text::Wrap::columns = 44;
print FOUT wrap('','' ,<FIN>);

它使所有列到44个字符而没有工作中断。但它在开始时减少了一些线的空间。

例如:在使用wrap()之前,如果一行在开头有5个空格,则在使用wrap()后,空格变为3。

任何人都可以帮助我。

2 个答案:

答案 0 :(得分:1)

我在Text :: Wrap的POD文档中看到了这一点:

  

Text::Wrap::wrap()通过将其输入中的所有选项卡扩展为空格来开始工作。最后一件事就是将空格转回标签。如果您不希望在结果中添加标签,请将$Text::Wrap::unexpand设置为false值。同样,如果您不想使用8个字符的tabstops,请将$Text::Wrap::tabstop设置为您希望用于tabstops的字符数。

这可以解释在行开头删除制表符的空格。您需要使用$Text::Wrap::tabstop$Text::Wrap::unexpand来查看是否可以获得所需的结果。

您可能想尝试Text::FormatText::Wrapper

答案 1 :(得分:0)

如果我正确理解你的问题,Text :: Wrap(不是不合理地)删除字符串前面的额外空格。你不希望它发生

这是一个用下划线替换前面的空格,通过wrap()运行然后删除下划线的解决方案

use Text::Wrap;

#test data, note the 5 spaces at the start of some lines
for (1..4) { push @a,"     hello I love you", "Won't you tell me your name? ";} 
push @a,"She's walking down the street","     Blind to every eye she meets","Do you think you\'ll be the guy", "To make the queen of the angels sigh?";

#make it longer with repeating
push @b,@a,@a,@a;

$Text::Wrap::columns = 44;

#show bad behaviour
print "VERSION 1\n";
print wrap("","",@b);

#fixup strings with leading underscores
map {s/^\s+/"_" x length(\1)/e} @b;

@z=wrap("","",@b);

#remove _
map{s/_+/" " x length(\1)/ge} @z;
print "\nVERSION 2\n";
print @z;