From: Hugo van der Sanden Date: Wed, 17 Jul 2002 23:36:20 +0000 (+0100) Subject: Re: sprintf: fix and docs X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a472f20992c59c78d978c548ff03c69e1ea7dffe;p=p5sagit%2Fp5-mst-13.2.git Re: sprintf: fix and docs Message-Id: <200207172236.g6HMaKU16388@crypt.compulink.co.uk> p4raw-id: //depot/perl@17614 --- diff --git a/pod/perlfunc.pod b/pod/perlfunc.pod index 0652b4e..40e2dd0 100644 --- a/pod/perlfunc.pod +++ b/pod/perlfunc.pod @@ -4952,7 +4952,7 @@ the join string using eg C<*2$v>: Arguments are usually formatted to be only as wide as required to display the given value. You can override the width by putting a number here, or get the width from the next argument (with C<*>) -or from a specified argument (with eg C<2$>): +or from a specified argument (with eg C<*2$>): printf '<%s>', "a"; # prints "" printf '<%6s>', "a"; # prints "< a>" @@ -5025,6 +5025,37 @@ for compatibility with XS code; it means 'use the standard size for a Perl integer (or floating-point number)', which is already the default for Perl code. +=item order of arguments + +Normally, sprintf takes the next unused argument as the value to +format for each format specification. If the format specification +uses C<*> to require additional arguments, these are consumed from +the argument list in the order in which they appear in the format +specification I the value to format. Where an argument is +specified using an explicit index, this does not affect the normal +order for the arguments (even when the explicitly specified index +would have been the next argument in any case). + +So: + + printf '<%*.*s>', $a, $b, $c; + +would use C<$a> for the width, C<$b> for the precision and C<$c> +as the value to format, while: + + print '<%*1$.*s>', $a, $b; + +would use C<$a> for the width and the precision, and C<$b> as the +value to format. + +Here are some more examples - beware that when using an explicit +index, the C<$> may need to be escaped: + + printf "%2\$d %d\n", 12, 34; # will print "34 12\n" + printf "%2\$d %d %d\n", 12, 34; # will print "34 12 34\n" + printf "%3\$d %d %d\n", 12, 34, 56; # will print "56 12 34\n" + printf "%2\$*3\$d %d\n", 12, 34, 3; # will print " 34 12\n" + =back If C is in effect, the character used for the decimal diff --git a/sv.c b/sv.c index 2c31193..fe7c0d4 100644 --- a/sv.c +++ b/sv.c @@ -7777,7 +7777,7 @@ Perl_sv_vcatpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV We allow format specification elements in this order: \d+\$ explicit format parameter index [-+ 0#]+ flags - v|*(\d+\$)?v vector with optional (optionally specified) arg + v|\*(\d+\$)?v vector with optional (optionally specified) arg \d+|\*(\d+\$)? width using optional (optionally specified) arg \.(\d*|\*(\d+\$)?) precision using optional (optionally specified) arg [hlqLV] size