Even though it is in double quotes, and %s should be interpolated to a non-existent hash, this is valid Perl and outputs "confusing = true".

#!/usr/bin/perl -w
use strict;

my $what = "confusing = %s";
printf $what, "true";

However, this is not valid (as expected), because $s does not exist:

my $what = "confusing = $s";
有帮助吗?

解决方案

You say "%s should be interpolated to a non-existent hash" but it shouldn't. There is no hash interpolation. It is possible in Perl 6 though.

其他提示

Also, hashes are not interpolated in double-quotes; scalars (including hash elements) and arrays are.

You're passing a format specifier and a value to printf, and it's behaving as intended; this may look like a hash interpolation, but it isn't.

See perldoc -f sprintf for details on format specifiers; in short, %s in the format specifier indicates that a value should be interpolated as a string, the effect being identical in this case to print "confusing = $what".

If you replace printf with print, the %s will be taken literally, rather than as a variable interpolation, and the result will be confusing = %s; this, and not a call to printf, is the case in which Torkington's dictum applies.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top