@@ -312,104 +312,14 @@ Examples:
312
312
"Perl".flip; # lreP
313
313
"ABBA".flip; # ABBA
314
314
315
- = head2 sub printf
316
-
317
- multi sub printf(Str:D $format, *@args)
318
-
319
- This function is mostly identical to the C library printf function.
320
-
321
- The C < $format > is scanned for C < % > characters. Any C < % > introduces a
322
- format token. Format tokens have the following grammar:
323
-
324
- grammar Str::PrintfFormat {
325
- regex format_token { '%': <index>? <precision>? <modifier>? <directive> }
326
- token index { \d+ '$' }
327
- token precision { <flags>? <vector>? <precision_count> }
328
- token flags { <[ \x20 + 0 \# \- ]>+ }
329
- token precision_count { [ <[1..9]>\d* | '*' ]? [ '.' [ \d* | '*' ] ]? }
330
- token vector { '*'? v }
331
- token modifier { < ll l h V q L > }
332
- token directive { < % c s d u o x e f g X E G b p n i D U O F > }
333
- }
334
-
335
- Directives guide the use (if any) of the arguments. When a directive
336
- (other than C < % > ) is used, it indicates how the next argument
337
- passed is to be formatted into the string to be printed.
338
-
339
- The directives are:
340
-
341
- = begin table
342
-
343
- % a literal percent sign
344
- c a character with the given codepoint
345
- s a string
346
- d a signed integer, in decimal
347
- u an unsigned integer, in decimal
348
- o an unsigned integer, in octal
349
- x an unsigned integer, in hexadecimal
350
- e a floating-point number, in scientific notation
351
- f a floating-point number, in fixed decimal notation
352
- g a floating-point number, in %e or %f notation
353
- X like x, but using uppercase letters
354
- E like e, but using an uppercase "E"
355
- G like g, but with an uppercase "E" (if applicable)
356
- b an unsigned integer, in binary
357
-
358
- = end table
359
-
360
- Compatibility:
361
-
362
- = begin table
363
-
364
- i a synonym for %d
365
- D a synonym for %ld
366
- U a synonym for %lu
367
- O a synonym for %lo
368
- F a synonym for %f
369
-
370
- = end table
371
-
372
- Perl 5 (non-)compatibility:
373
-
374
- = begin table
375
-
376
- n produces a runtime exception
377
- p produces a runtime exception
378
-
379
- = end table
380
-
381
- Modifiers change the meaning of format directives, but are largely
382
- no-ops (the semantics are still being determined).
383
-
384
- = begin table
385
-
386
- h interpret integer as native "short" (typically int16)
387
- l interpret integer as native "long" (typically int32 or int64)
388
- ll interpret integer as native "long long" (typically int64)
389
- L interpret integer as native "long long" (typically uint64)
390
- q interpret integer as native "quads" (typically int64 or larger)
391
-
392
- = end table
393
-
394
- Examples:
395
-
396
- printf('%c', 97); # a
397
- printf("%.2f", 1.969); # 1.97
398
- printf("%+.3f", 3.141592); # +3.142
399
- printf('%2$d %1$d', 12, 34); # 34 12
400
- printf("%x", 255); # ff
401
-
402
- Special case: printf("<b>%s</b>\n", "Perl 6") will not work use either of the following:
403
-
404
- printf Q:b "<b>%s</b>\n", "Perl 6";
405
- printf "<b>\%s</b>\n", "Perl 6";
406
- printf "<b>%s\</b>\n", "Perl 6";
407
-
408
315
= head2 sub sprintf
409
316
410
317
multi sub sprintf( Str:D $format, *@args) returns Str:D
411
318
412
- This function is mostly identical to the C library sprintf function.
319
+ This function is mostly identical to the C library C < sprintf > and
320
+ C < printf > functions. The only difference between the two
321
+ functions is that C < sprintf > returns a string while the C < printf > function
322
+ writes to a file.
413
323
414
324
The C < $format > is scanned for C < % > characters. Any C < % > introduces a
415
325
format token. Format tokens have the following grammar:
@@ -427,7 +337,7 @@ format token. Format tokens have the following grammar:
427
337
428
338
Directives guide the use (if any) of the arguments. When a directive
429
339
(other than C < % > ) is used, it indicates how the next argument
430
- passed is to be formatted into the string.
340
+ passed is to be formatted into the string to be printed .
431
341
432
342
The directives are:
433
343
@@ -488,12 +398,19 @@ Examples:
488
398
489
399
= for code :skip-test
490
400
sprintf "%ld a big number, %lld a bigger number\n", 4294967295, 4294967296;
401
+ sprintf('%c', 97); # a
402
+ sprintf("%.2f", 1.969); # 1.97
403
+ sprintf("%+.3f", 3.141592); # +3.142
404
+ sprintf('%2$d %1$d', 12, 34); # 34 12
405
+ sprintf("%x", 255); # ff
491
406
492
- Special case: sprintf("<b>%s</b>\n", "Perl 6") will not work use either of the following:
407
+ Special case: 'sprintf("<b>%s</b>\n", "Perl 6")' will not work, but
408
+ one of the following will:
493
409
494
- sprintf Q:b "<b>%s</b>\n", "Perl 6";
495
- sprintf "<b>\%s</b>\n", "Perl 6";
496
- sprintf "<b>%s\</b>\n", "Perl 6";
410
+ = for code :skip-test
411
+ sprintf Q:b "<b>%s</b>\n", "Perl 6";
412
+ sprintf "<b>\%s</b>\n", "Perl 6";
413
+ sprintf "<b>%s\</b>\n", "Perl 6";
497
414
498
415
= head2 method starts-with
499
416
0 commit comments