为什么 printf 和 sprintf 在仅给定数组时表现不同? [英] Why do printf and sprintf behave differently when only given an array?

查看:62
本文介绍了为什么 printf 和 sprintf 在仅给定数组时表现不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

sub do_printf  { printf @_ }
sub do_sprintf { print sprintf @_ }

do_printf("%s\n", "ok");  # prints ok
do_sprintf("%s\n", "ok"); # prints 2

推荐答案

来自 perldoc on sprintf:

与 printf 不同,sprintf 不做当你通过时你可能是什么意思它是一个数组作为你的第一个参数.数组被赋予标量上下文,并且而不是使用的第 0 个元素数组作为格式,Perl 将使用数组中元素的计数为格式,几乎从不有用.

Unlike printf, sprintf does not do what you probably mean when you pass it an array as your first argument. The array is given scalar context, and instead of using the 0th element of the array as the format, Perl will use the count of elements in the array as the format, which is almost never useful.

这篇关于为什么 printf 和 sprintf 在仅给定数组时表现不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆