Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

$_ as default printf arg fails

by Neutron Jack (Pilgrim)
on Mar 20, 2013 at 23:55 UTC ( #1024633=perlquestion: print w/replies, xml ) Need Help??
Neutron Jack has asked for the wisdom of the Perl Monks concerning the following question:

When I run this code using Perl v5.14.2 on Ubuntu:

#! /usr/bin/perl use strict; use warnings; $_ = 42; printf "%d\n";
I get: Missing argument in printf at ./p line 5.

However, perldoc -f printf says that it should work:

If you omit the LIST, $_ is used;

Replies are listed 'Best First'.
Re: $_ as default printf arg fails
by toolic (Bishop) on Mar 21, 2013 at 00:17 UTC
    I agree that the doc seems to be wrong. The doc even implies that the following should do something, but I still get a warning:

    The current printf (16.2) has the same information. This may be worthy of a perlbug.

    UPDATE: This doc first appeared on 14.2 (it wasn't there on 14.0, or before), but I don't see a change to printf behavior in perl5141delta or perl5142delta.

    It seems the warning is triggered if $_ contains %

    $_ = "%d 42"; printf;

      I believe that the document is correct. The default $_ is treated as a FORMAT. The warning is triggered (or not) the same as it is for an explicit FORMAT. Subtle isn't it?

Re: $_ as default printf arg fails
by kcott (Chancellor) on Mar 21, 2013 at 07:55 UTC

    G'day Neutron Jack,

    While I agree the documentation for printf is not really up to scratch; I also think your comments are based on a false premise.

    The documentation shows four forms that the function can take; two have no LIST (printf FILEHANDLE and printf):

    $ perl -Mstrict -Mwarnings -E '$_ = 42; printf STDERR' 42 $ perl -Mstrict -Mwarnings -E '$_ = 42; printf' 42

    However, the documentation uses list and LIST in an ambiguous manner; furthermore, use of the lowercase list is, at best, misleading (but, more likely, an error).

    list (lowercase)

    An equivalence is shown:

    print FILEHANDLE sprintf(FORMAT, LIST)

    followed by "... The first argument of the list will be interpreted as the printf format."

    My best guess is that this is supposed to refer to the list of arguments to sprintf — but that's all that is, a complete guess. The documentation would be greatly improved if that was clarified.

    LIST (uppercase)

    The "If you omit the LIST, $_ is used; to use FILEHANDLE without a LIST, ..." text follows the sprintf ambiguity. I believe this refers to the two printf forms (shown at the top) which do not contain LIST. Without the confusing text before it, I think would be somewhat more obvious; however, additional clarification might not go astray here either.

    -- Ken

Re: $_ as default printf arg fails
by LanX (Bishop) on Mar 21, 2013 at 08:35 UTC

    I think kcott is on the right track, it's a documentation error.


    If you omit the LIST, $_ is used; to use FILEHANDLE without a LIST, you must use a real filehandle like FH , not an indirect one like $fh

    LIST is meant to describe the ARGUMENTS ( i.e. FORMAT, VALUES) to the function (or method i.e. FILEHANDLE->printf(ARGUMENTS) ), like you can see in the following example

    DB<100> $_=42 => 42 DB<101> printf 42 DB<102> printf STDOUT 42

    (tested with 5.10 which didn't even document the use of $_)

    So LIST should be replaced with "ARGUMENTS" in the quoted part.

    IMHO the other occurrences of LIST should be replaced with "VALUES".

    that is


    Cheers Rolf

    ( addicted to the Perl Programming Language)

Re: $_ as default printf arg fails
by ww (Archbishop) on Mar 21, 2013 at 01:22 UTC
    Not a cure, and maybe still a bug, but docs call for a comma between format and list.

    If you didn't program your executable by toggling in binary, it wasn't really programming!

Re: $_ as default printf arg fails
by nvivek (Vicar) on Mar 21, 2013 at 04:40 UTC

    As per perl document, printf function used without arguments will take value of $_ and it gets printed. When you specify format, need to give LIST argument and it won't take $_ value by default.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1024633]
Approved by toolic
Front-paged by LanX
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2018-05-24 00:44 GMT
Find Nodes?
    Voting Booth?