Perl 6::Form中的文本换行

3zwjbxry  于 12个月前  发布在  Perl
关注(0)|答案(1)|浏览(83)

Perl6::Form中,是否可以在任何字符处换行,而不仅仅是空格?

| Item                                    |
| {[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[} |
  @phrase,

以上格式生成,

| Item                                    |
| Word1                                   |
| Super_________long________________word1 |

我想让它把单词本身打断。有可能吗?就像下面这样

| Item                                    |
| Word1 Super_________long_______________ |
| _word1                                  |

另外,在这个例子中,为什么blah2 or 1.3 or 101.0没有与表12中相应的值对齐

my @name=();
my @score=();
my @time=();
my @normalized=();

push(@name,'blah1');
push(@score,'115555555');
push(@time,1.1);
push(@normalized,100);
push(@name,'blah2');
push(@score,'12');
push(@time,1.3);
push(@normalized,101);

print form
    '-------------------------------------------',
    'Name             Score   Time  | Normalized',
    '-------------------------------------------',
    '{[[[[[[[[[[[[}   {[[[}  {II}  |  {]]].[[} ',
     \@name,          \@score,\@time,  \@normalized; 


-------------------------------------------
Name             Score   Time  | Normalized
-------------------------------------------
blah1            1155-  1.1   |   100.0
blah2            55555  1.3   |   101.0
                 12           |

从文档中,我了解到可以使用布局和中断选项来实现所需的行为

print form
{layout=>"tabular"},
{break=>\&break_width},

但是文档中发布的函数有编译问题:

sub break_width {
    my ($data_ref, $width, $ws) = @_;
    for ($$data_ref) {
        # Treat any squeezed or vertical whitespace as a single character
        # (since they'll subsequently be squeezed to a single space)
        my $single_char = qr{ $ws | [\n\r]+ | . }
 
        # Give up if there are no more characters to grab...
        return ("", 0) unless m/\G (single_char{1,$width}) /gcx;
 
        # Squeeze the resultant substring...
        (my $result = $1) =~ s/ $ws | [\n\r] / /gx;
 
        # Check for any more data still to come...
        my $more = m/\G (?= .* \S) /gcx;
 
        # Return the squeezed substring and the "more" indicator...
        return ($result, $more);
    }
}

在修复了这个函数的基本问题之后,我仍然遇到未初始化的$ws。很明显这不是测试代码。我错过了什么吗?

cngwdvgl

cngwdvgl1#

关于docs

换行

每当一个字段传递的数据超过单行所能容纳的数据时,form就被迫在某个地方“中断”这些数据。
如果有问题的字段是W列宽的,form首先压缩所有空格(由用户的ws选项指定),然后查看字符串的下一个W列。
然后,form的breaking算法搜索换行符、回车符、任何其他空格字符或连字符。如果它在前W列中找到换行符或回车符,它会立即在该点断开数据字符串。否则,它会定位前W列中的最后一个空格或连字符,并在该空格或连字符之后立即断开字符串。如果找不到合适的地方断开字符串时,它会在第(W-1)列处将其断开并追加一个连字符。

相关问题