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

In testing a new install of Perl 5.8 (previously 5.6.1) I ran into several scripts that bombed!
The offending scripts all had similar sprintf lines something like:

my $tot = sprintf "%lf\n", ($ARGV[0]/$ARGV[1]);

The error I got was:

Invalid conversion in sprintf: "%f" at yadayadayada line whatever

Now if I change the line to:

my $tot = sprintf "%f\n", ($ARGV[0]/$ARGV[1]);
I no longer get the error but I'm not 100% sure I understand the difference. I don't get this error with 5.6.1. This being said, I have 2 questions. What is the difference (if any) between "%lf" and "%f" as a format to (s)printf? And is there a concise document that outlines all the syntactical changes in Perl 5.8? I have read perldelta and I did see a number of things but not specifically this issue. So, it leaves me wondering if there may be other changes not listed in perldelta.

Thanks!

Replies are listed 'Best First'.
Re: Syntactical changes in Perl 5.8 from 5.6.1
by Anonymous Monk on Oct 10, 2003 at 17:45 UTC
      Well That's a revelation ... At least I'm in good company.
      Thanks!
Re: Syntactical changes in Perl 5.8 from 5.6.1
by Abigail-II (Bishop) on Oct 10, 2003 at 16:31 UTC
    I don't have 5.6.1 available right now, so I can't test it, but what is "%lf" doing? In 5.8.x, 'l' is a size specifier for *integers* (the "d u o x X b i D U O" formats), forcing Perl to interpret them as longs. For %f, you can use the q, L or ll size specifier (quads or long long), but not l.

    As for the more general question, differences between 5.8.0 and 5.6.1 are found in the perldelta manual page that comes with 5.8.0. For 5.8.1, the differences between 5.8.0 and 5.8.1 are in its perldelta manual page (and things new in 5.8.0 are found in perl58delta.

    Abigail

      According to 'perldoc -f sprintf'(Perl 5.6.1) the "l" flag used between % and the conversion letter, interprets an integer as C type "long" or "unsigned long".
      I must admit that I am a bit mathematically challenged so, I'm still foggy on the difference between a "floating point number(%f) and a long floating point number(%lf). Any further clarification or documentation would be greatly appriciated.

      Also, I still don't see anything in the perldelta manpage that either deprecates the "%lf" or mentions that there has been a change in syntax.

      Thanks for your assistance!

        Indeed, it interprets an integer as C type "long" or "unsigned long". But %f is a conversion for floats, not integers.

        Also, I still don't see anything in the perldelta manpage that either deprecates the "%lf" or mentions that there has been a change in syntax.
        It actually might be this:
        o The Gconvert macro ($Config{d_Gconvert}) used by perl for stringifying floating-point numbers is now more picky about using sprintf %.*g rules for the conver- sion. Some platforms that used to use gcvt may now resort to the slower sprintf.

        But I'm not sure. And not everything is documented, the people working on Perl are only human!

        Abigail

Re: Syntactical changes in Perl 5.8 from 5.6.1
by sgifford (Prior) on Oct 10, 2003 at 16:59 UTC
    Just a thought: I think that perl can be compiled to use your system's printf function, or its own internal one. Perhaps your old version was compiled to use your system printf, which recognized %lf (perhaps as an alternative to %Lf, which is a long double), and the new version uses perl's printf, which doesn't allow this.
      That's an interesting take, I think both were installed from an ActiveState binary. ++