您的報價已關閉。
perl -E 'say sprintf(q{%1$s%2$s is not %1$s and not %2$s}, "foo", "bar");'
foobar is not foo and not bar
,因爲你的shell會很困惑,你不能使用的-e
雙引號""
。你需要單引號。但是如果使用printf
模式的雙引號和%1$s
語法,Perl會嘗試插入$s
,這不起作用。因此請使用非引用的q{}
或使用\'
轉義單引號''
。或者逃脫$
s。
如果打開use strict
和use warnings
你會看到:
$ perl -E 'use strict; use warnings; say sprintf("%1$s%2$s is not %1$s and not %2$s", "foo", "bar");'
Global symbol "$s" requires explicit package name at -e line 1.
Global symbol "$s" requires explicit package name at -e line 1.
Global symbol "$s" requires explicit package name at -e line 1.
Global symbol "$s" requires explicit package name at -e line 1.
Execution of -e aborted due to compilation errors.
與單引號''
爲-e
和雙引號""
爲模式的。
$ perl -E "use strict; use warnings; say sprintf('%1$s%2$s is not %1$s and not %2$s', 'foo', 'bar');"
Invalid conversion in sprintf: "%1 " at -e line 1.
Invalid conversion in sprintf: "%2" at -e line 1.
%2 is not %1 and not %2
現在殼試圖插$s
因爲雙引號""
的。所以Perl永遠不會看到它。它認爲該模式爲"%1 %2 is not %1 and not %2"
,它無法理解。 (請注意,%
不會在Perl中用雙引號字符串插入)。
@HunterMcMillen,你的意思是創建一個函數? ''perl -e'sub ppp(){printf('%1 $ s%2 $ s不是%1 $ s而不是%2 $ s','foo','bar');} ppp();「 「像這樣?這不起作用。 – Kirby