10.4. printf Formats Don't Impose Context

If you're a die-hard C programmer (and is there any other kind?), you might think that to print the number of entries in an array, since the %d printf format specifier means “number,” well gosh, a number is a scalar, and so that's the context it'll put its argument into:

printf "Number of entries in \@foo = %d", @foo;

but by now you know us too well to fall for that … yes, @foo is in list context here, because that's what printf is prototyped to take. So you'll either get the first entry of @foo printed, or a complaint if that's not a number.

Get Perl Debugged now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.