代码之家  ›  专栏  ›  技术社区  ›  RedGrittyBrick

Perl5中“write”和“format”的替代品是什么?

  •  2
  • RedGrittyBrick  · 技术社区  · 14 年前

    对于简单的输出格式,我倾向于使用printf,在Perl 4时代我使用write/format。然而,有时对于每个数据记录输出行的可变数目,它似乎是最简单的解决方案。例如

    #!/usr/bin/perl
    use strict;
    use warnings;
    
    my ($lorem, $aprille);
    
    format =
    @# ^<<<<<<<<<<<<<<<<<<<<<<<< | ^<<<<<<<<<<<<<<<<<<
    $.,$aprille                  , $lorem
       ^<<<<<<<<<<<<<<<<<<<<<<<< | ^<<<<<<<<<<<<<<<<<< ~~
       $aprille                  , $lorem
                                 |
    .
    
    while(<DATA>) {
      ($aprille, $lorem) = split(/\|/, $_, 2);
      write;
    }
    
    __DATA__
    WHAN that Aprille with his shoures soote       |Lorem ipsum dolor sit amet,
    The droghte of Marche hath perced to the roote,|consectetur adipisicing elit,
    And bathed every veyne in swich licour,        |sed do eiusmod tempor
    Of which vertu engendred is the flour;         |incididunt ut labore et dolore
    Whan Zephirus eek with his swete breeth        |magna aliqua. Ut enim ad minim
    Inspired hath in every holt and heeth          |veniam, quis nostrud
    The tendre croppes, and the yonge sonne        |exercitation exercitation
    Hath in the Ram his halfe cours y-ronne,       |ullamco laboris nisi ut ali-
    And smale fowles maken melodye,                |quip ex ea commodo conse-
    That slepen al the night with open ye,         |quat. Duis aute irure dolor
    So priketh hem nature in hir corages:          |in reprehenderit in volup-
    Than longen folk to goon on pilgrimages,       |tate velit esse cillium dol-
    And palmers for to seken straunge strondes,    |ore eu fugiat nulla pariatur.
    To ferne halwes, couthe in sondry londes;      |Lorem ipsum dolor sit amet,
    And specially, from every shires ende          |consectetur adipisicing elit,
    Of Engelond, to Caunterbury they wende,        |sed do eiusmod tempor
    The holy blisful martir for to seke,           |incididunt ut labore et dolore
    That hem hath holpen, whan that they were seke.|magna aliqua. Ut enim ad minim
    And now for something completely different. Nice plumage.|Norwegian blue.
    

    生产

       1 WHAN that Aprille with    | Lorem ipsum dolor
         his shoures soote         | sit amet,
                                   |
       2 The droghte of Marche     | consectetur
         hath perced to the roote, | adipisicing elit,
                                   |
       3 And bathed every veyne in | sed do eiusmod
         swich licour,             | tempor
      ...
      19 And now for something     | Norwegian blue.
         completely different.     |
         Nice plumage.             |
    

    线。

    2 回复  |  直到 14 年前
        1
  •  2
  •   Chas. Owens    14 年前

    格式的主要问题是对全局变量的依赖。有关格式的其他问题,请参见 Perl Best Practices

    现代的解决方案是 Perl6::Form

    下面是格式代码到 Perl6::窗体 . 我不知道 很好,所以可能有一些方法可以使它更好或更真实地与原来的例子:

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    use Perl6::Form;
    
    while(<DATA>) {
        my ($aprille, $lorem) = split(/\|/, $_, 2);
        print form(
            "{>} {[[[[[[[[[[[[[[[[[[[[[[[[} | {[[[[[[[[[[[[[[[[[[}",
            $.,  $aprille,                  $lorem,
            "                               |                     ",
        );
    }
    
    __DATA__
    WHAN that Aprille with his shoures soote       |Lorem ipsum dolor sit amet,
    The droghte of Marche hath perced to the roote,|consectetur adipisicing elit,
    And bathed every veyne in swich licour,        |sed do eiusmod tempor
    Of which vertu engendred is the flour;         |incididunt ut labore et dolore
    Whan Zephirus eek with his swete breeth        |magna aliqua. Ut enim ad minim
    Inspired hath in every holt and heeth          |veniam, quis nostrud
    The tendre croppes, and the yonge sonne        |exercitation exercitation
    Hath in the Ram his halfe cours y-ronne,       |ullamco laboris nisi ut ali-
    And smale fowles maken melodye,                |quip ex ea commodo conse-
    That slepen al the night with open ye,         |quat. Duis aute irure dolor
    So priketh hem nature in hir corages:          |in reprehenderit in volup-
    Than longen folk to goon on pilgrimages,       |tate velit esse cillium dol-
    And palmers for to seken straunge strondes,    |ore eu fugiat nulla pariatur.
    To ferne halwes, couthe in sondry londes;      |Lorem ipsum dolor sit amet,
    And specially, from every shires ende          |consectetur adipisicing elit,
    Of Engelond, to Caunterbury they wende,        |sed do eiusmod tempor
    The holy blisful martir for to seke,           |incididunt ut labore et dolore
    That hem hath holpen, whan that they were seke.|magna aliqua. Ut enim ad minim
    And now for something completely different. Nice plumage.|Norwegian blue.
    
        2
  •  3
  •   tchrist    14 年前

    perl5加在perl4之上的主要内容 format write formline $^A ,数字格式和包作用域,但这些主要是不必要的绒毛。当前的格式化指令集只比perl4s大一点:

    @    start of regular field
    ^    start of special field
    <    pad character for left justification
    |    pad character for centering
    >    pad character for right justification
    #    pad character for a right justified numeric field
    0    instead of first #: pad number with leading zeroes
    .    decimal point within a numeric field
    ...  terminate a text field, show "..." as truncation evidence
    @*   variable width field for a multi-line value
    ^*   variable width field for next line of a multi-line value
    ~    suppress line with all fields empty
    ~~   repeat line until all fields are exhausted
    

    其他鲜为人知的增强功能包括支持LC_NUMERIC local,能够使用 {} \r

    我仍然使用 格式 时不时的。这是我几周前写的一个程序的一部分。

    sub init_screen() {
        our %Opt;
        my  $cols;
    
        if ($Opt{width}) {
             $cols  = $Opt{width};
        } 
        elsif (am_unixy()) {
            ($cols) = `stty size 2>&1` =~ /^\d+ (\d+)$/;
        }
        else {
             # FALLTHROUGH to ||= init on next line
        }
    
        $cols ||= 80; # non-unix or stty error
        $cols  -=  2;
    
        my $format  = "format STDOUT = \n"
                    . '    ^'    . '<' x ($cols-4) . "\n"
                    . '$_' . "\n"
                    . "       ^" . "<" x ($cols-6) . "~~\n"
                    . '$_' . "\n"
                    . ".\n"
                    . "1;" # for true eval return
                    ;
    
        eval($format) || die;
    }
    

    根据当前屏幕宽度动态调整可能更漂亮,但仍然有用。