in reply to Re: printf size flag incompatibility
in thread printf size flag incompatibility

Summary: you're using h and l incorrectly

Thanks for the info. But to be clear, I was exploring l, h, etc. exploratively and unfortunately only showed a floating point example. The exact same differences (between 5.6.1 and 5.8.0) show up when used for integer conversions.

# file: printf.pl printf "%4.4f\n", 1; printf "%4.4lf\n", 1; printf "%4.4hf\n", 1; printf "%4.4qf\n", 1; printf "%4.4Lf\n", 1; printf "%4.4llf\n", 1; #outputs: $ /usr/bin/perl printf.pl 1.0000 1.0000 1.0000 %4.4qf %4.4Lf %4.4llf $ /usr/local/bin/perl printf.pl 1.0000 %4.4lf %4.4hf %4.4qf 1.0000 1.0000

Where /usr/bin/perl is 5.6.1 and /usr/local/bin/perl is 5.8.0 with the same compile flags (other than ithreads as previously mentioned) and the same version of gcc under linux. It was an integer conversion usage in Damian's Text::Reformat module that brought this to light. Something funny is definitely going on.

Replies are listed 'Best First'.
Re: Re: Re: printf size flag incompatibility
by Ionitor (Scribe) on Jul 22, 2002 at 12:28 UTC
    printf doesn't really care whether the arguments are integers or not, it cares about what the size flags immediately precede. If you use the following code:
    use warnings; printf "%4.4i\n", 1; printf "%4.4li\n", 1; printf "%4.4hi\n", 1; printf "%4.4qi\n", 1; printf "%4.4Li\n", 1; printf "%4.4lli\n", 1;
    You then almost get the results that are predicted in the perldocs:
    0001 0001 0001 Invalid conversion in printf: "%q" at - line 5. %4.4qi 0001 0001
    (the zeroes are there because 4.4 ensures that the number will be 4 digits long in the case of an int)

    Of course, there's still that rogue q that doesn't work in 5.6.1 or 5.8.0. However, since q should be the same as L and ll, it's not like Perl is lacking some feature--they just lie about q being a valid alias.

    Again: l and h should only be used when followed by i, never by f. Perl, C, and GNU all appear to agree on this.