Perl 不一致地打印包含 '%' 特定组合的字符串

jav*_*ase 2 perl printf

谁能解释一下我遇到的这种 perl 行为?

printf("%what_the\n");
printf("\%what_the\n");
Run Code Online (Sandbox Code Playgroud)

印刷:

%what_the
%what_the
Run Code Online (Sandbox Code Playgroud)

尽管...

%what_the
%what_the
Run Code Online (Sandbox Code Playgroud)

印刷:

0morrow
0morrow
Run Code Online (Sandbox Code Playgroud)

即使有 wanrings 和严格:

use strict;
use warnings;
printf("\%tomorrow\n");
Run Code Online (Sandbox Code Playgroud)

印刷:

Missing argument in printf at - line 3.
0morrow
Run Code Online (Sandbox Code Playgroud)

TLP*_*TLP 7

printf与常规不同print。你可能认为它是一样的,它不是。printf采用一个模式,其中包括%. 例如:

printf "%s\n", "tomorrow";   # prints "tomorrow\n"
Run Code Online (Sandbox Code Playgroud)

%s是字符串的占位符,它应该是 的第二个参数printf

您收到的警告显示了问题所在

Missing argument in printf at - line 3.
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

%to在那里看不到,但似乎是被触发的。它打印 a0因为它将空字符串(缺少参数)转换为0

文档在这里