gpt4 book ai didi

perl - Perl 5 中 `write` 和 `format` 的替代品是什么?

转载 作者:行者123 更新时间:2023-12-02 15:41:39 25 4
gpt4 key购买 nike

对于简单的输出格式,我倾向于使用 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. |

请注意,记录 19 占用了 行。

在不使用 write 和 format 的情况下,有什么等效的简洁的 perl5ish 方法?

最佳答案

perl5在perl4的formatwrite之上增加的主要是formline。还有一些其他的细节,包括 $^A、数字格式和包范围,但这些主要是无关紧要的。当前的格式化指令集仅比 perl4 的多一点:

@    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 本地,能够使用 {} 分隔的 block 来帮助对齐,以及使用 \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;
}

根据当前屏幕宽度动态构建 format 的代码可能更漂亮,但它仍然有用。

关于perl - Perl 5 中 `write` 和 `format` 的替代品是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4134823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com