summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo van der Sanden <hv@crypt.org>2002-07-18 00:36:20 +0100
committerJarkko Hietaniemi <jhi@iki.fi>2002-07-17 21:51:10 +0000
commita472f20992c59c78d978c548ff03c69e1ea7dffe (patch)
tree9858ec50b6f62f51b40be8a5a10c114a59ce0433
parentc6261f3b8f19d9323284da1161283a11e2d7d63a (diff)
downloadperl-a472f20992c59c78d978c548ff03c69e1ea7dffe.tar.gz
Re: sprintf: fix and docs
Message-Id: <200207172236.g6HMaKU16388@crypt.compulink.co.uk> p4raw-id: //depot/perl@17614
-rw-r--r--pod/perlfunc.pod33
-rw-r--r--sv.c2
2 files changed, 33 insertions, 2 deletions
diff --git a/pod/perlfunc.pod b/pod/perlfunc.pod
index 0652b4edbb..40e2dd0491 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 "<a>"
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<before> 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<use locale> is in effect, the character used for the decimal
diff --git a/sv.c b/sv.c
index 2c31193985..fe7c0d4607 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