Perl不一致地打印包含'%'特定组合的字符串
谁能解释一下我遇到的这种 perl 行为?
printf("%what_then");
printf("%what_then");
印刷:
%what_the
%what_the
%what_the
%what_the
尽管...
印刷:
0morrow
0morrow
即使有 wanrings 和严格:
use strict;
use warnings;
printf("%tomorrown");
印刷:
Missing argument in printf at - line 3.
0morrow
回答
printf与常规不同print。你可能认为它是一样的,它不是。printf采用一个模式,其中包括%. 例如:
printf "%sn", "tomorrow"; # prints "tomorrown"
%s是字符串的占位符,它应该是 的第二个参数printf。
您收到的警告显示了问题所在
Missing argument in printf at - line 3.
printf 需要第二个参数,因为您提供了一个占位符。
并非百分号后面的所有字母都是有效组合,以下是来自文档的一些 sprintf
%% a percent sign
%c a character with the given number
%s a string
%d a signed integer, in decimal
%u an unsigned integer, in decimal
%o an unsigned integer, in octal
%x an unsigned integer, in hexadecimal
%e a floating-point number, in scientific notation
%f a floating-point number, in fixed decimal notation
%g a floating-point number, in %e or %f notation
.... more
我%to在那里看不到,但似乎是被触发的。它打印 a0因为它将空字符串(缺少参数)转换为0。
文档在这里。