Llew_Llaw_Gyffes has asked for the wisdom of the Perl Monks concerning the following question:

I'm in the habit of checking my code using perl -c -Mdiagnostics.

However, I'm also in the habit of typing, for example:

printf ("%-10s : %d\n", $foo, $bar[$i]);

I do it quite intentionally, because I'm used to thinking of printf() as a function, so I invoke it as one.  I also feel it makes my code more readable.  Unfortunately, diagnostics.pm does not share my coding style preferences, and insists on warning me that I've used printf as a function (why, yes, I did, imagine that) ... every single time I use printf.

Is there any way, short of modifying diagnostics.pm itself, to tell diagnostics.pm that I'm quite well aware I'm using printf as a function, and do not wish to be warned about it?

Replies are listed 'Best First'.
Re: Customizing diagnostics?
by ikegami (Patriarch) on Jul 20, 2005 at 04:34 UTC

    Your snippet alone is not enough to give the warning.

    printf ("%-10s : %d\n", $foo, $bar[$i]) && ...;
    does, because it ambiguous whether you want
    (printf ("%-10s : %d\n", $foo, $bar[$i])) && ...;

    or

    printf (("%-10s : %d\n", $foo, $bar[$i]) && ...);

    Using any of the following will remove the warning:

    printf("%-10s : %d\n", $foo, $bar[$i]) && ...; (printf "%-10s : %d\n", $foo, $bar[$i]) && ...; (printf ("%-10s : %d\n", $foo, $bar[$i])) && ...;

    By the way, diagnostics has nothing to do your problem. diagnostics explains warnings which occur whether diagnostics is loaded or not. To disable warnings, have a look at warnings. It's even possible to selectively disable warnings.

Re: Customizing diagnostics?
by ysth (Canon) on Jul 20, 2005 at 04:34 UTC
    Don't blame diagnostics; it doesn't add any warnings, it just explains them. I hope you are turning on warnings in your code even when not doing the -c thing. With a suitably non-decrepit version of perl, put a use warnings; no warnings "syntax"; at the top of your code, or switch to a non-GNU coding style, with no space between function name and left parenthesis.

    I know this warning is somewhat controversial, because it in effect is prejudiced against a common coding style, but for those of us who don't use that coding style and do call builtins and subs without (), it can be a lifesaver.

Re: Customizing diagnostics?
by Errto (Vicar) on Jul 20, 2005 at 04:35 UTC
    Try this:
    no warnings 'parenthesis';
    at the beginning of your code. See warnings and perllexwarn for more. Update: This may be wrong. See below.
      "parenthesis" is maybe what you would expect, but it is in fact only a "syntax" warning. The category is shown after the W at the start of the perldiag entry (which should be authoritative). "parenthesis" only applies to my or our followed by a paren-less list.
        Well, here's something a bit odd. Here's what I was trying:
        $ perl -wce'no warnings "parenthesis"; print ("\n")' -e syntax OK
        Which led me to think that my above reply was correct. If I change  no warnings "parenthesis"; to, say,  use strict; or chomp; or print "foo";, then the warning comes back. But if I change it to no warnings "y2k"; or even  use warnings "y2k" (or any other category from perllexwarn), it stays gone. I'm on 5.8.3.
      This is, indeed, wisdom of which I was not yet aware. I shall explore its mysteries.