ZetCode

Perl printf 函数

最后修改于 2025 年 4 月 4 日

Perl 的 printf 函数根据格式字符串生成格式化输出。它类似于 C 的 printf,并提供精确的控制。

printf 接受一个格式字符串,后跟要插入的值。格式指定了每个值应如何显示,包括宽度、精度和对齐。

基本 printf 用法

最简单的 printf 使用占位符格式化字符串。

basic.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

my $name = "Alice";
my $age = 28;

printf("Name: %s, Age: %d\n", $name, $age);

此示例演示了基本的字符串 (%s) 和十进制 (%d) 格式化。占位符按顺序被相应的变量替换。

$ ./basic.pl
Name: Alice, Age: 28

数字格式化

printf 提供了广泛的数字格式化选项。

numbers.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

my $price = 19.99;
my $quantity = 5;
my $total = $price * $quantity;

printf("Price: \$%.2f\n", $price);
printf("Quantity: %03d\n", $quantity);
printf("Total: \$%8.2f\n", $total);

我们将浮点数格式化为 2 位小数,用前导零填充整数,并控制字段宽度以进行对齐。

$ ./numbers.pl
Price: $19.99
Quantity: 005
Total: $   99.95

字符串格式化

字符串格式化控制字段宽度、填充和对齐。

strings.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

my $item1 = "Notebook";
my $item2 = "Pen";
my $price1 = 2.99;
my $price2 = 1.49;

printf("%-15s \$%5.2f\n", $item1, $price1);
printf("%-15s \$%5.2f\n", $item2, $price2);

- 标志将字符串左对齐在 15 个字符的字段中。数字在 5 个字符的字段中右对齐,并保留 2 位小数。

$ ./strings.pl
Notebook        $ 2.99
Pen             $ 1.49

十六进制和八进制输出

printf 可以以不同基数显示数字。

bases.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

my $num = 255;

printf("Decimal: %d\n", $num);
printf("Hexadecimal: %x\n", $num);
printf("Hexadecimal (uppercase): %X\n", $num);
printf("Octal: %o\n", $num);
printf("With prefixes: %#x %#o\n", $num, $num);

%x%X 格式输出十六进制,%o 输出八进制。# 标志添加基数前缀(十六进制为 0x,八进制为 0)。

$ ./bases.pl
Decimal: 255
Hexadecimal: ff
Hexadecimal (uppercase): FF
Octal: 377
With prefixes: 0xff 0377

科学计数法

大数或小数可以显示为科学计数法。

scientific.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

my $avogadro = 6.02214076e23;
my $electron_mass = 9.10938356e-31;

printf("Avogadro's number: %.4e\n", $avogadro);
printf("Electron mass: %.4e kg\n", $electron_mass);
printf("Alternative format: %.4g\n", $avogadro);

%e 使用科学计数法,而 %g 根据数字的量级在 %f%e 之间选择。精度控制有效数字。

$ ./scientific.pl
Avogadro's number: 6.0221e+23
Electron mass: 9.1094e-31 kg
Alternative format: 6.022e+23

可变宽度和精度

宽度和精度可以使用 * 动态指定。

dynamic.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

my $pi = 3.141592653589793;
my $width = 10;
my $precision = 4;

printf("Fixed: %.*f\n", $precision, $pi);
printf("Dynamic width: %*.*f\n", $width, $precision, $pi);

格式字符串中的 * 表示“将下一个参数用作此值”。这允许对格式参数进行运行时控制。

$ ./dynamic.pl
Fixed: 3.1416
Dynamic width:     3.1416

位置参数

参数可以在格式字符串中按位置引用。

positional.pl
#!/usr/bin/perl

use strict;
use warnings;
use v5.34.0;

my $name = "Bob";
my $age = 42;
my $score = 95.5;

printf("%2\$s is %1\$d years old and scored %3\$.1f%%\n", 
       $age, $name, $score);

n$ 语法指定使用哪个参数(基于 1 的索引)。这允许在格式字符串中重新排序或重用参数。

$ ./positional.pl
Bob is 42 years old and scored 95.5%

最佳实践

来源

Perl printf 文档

本教程介绍了 Perl 的 printf 函数,并提供了实用示例,演示了其强大的格式化功能。

作者

我叫 Jan Bodnar,我是一名充满热情的程序员,拥有丰富的编程经验。我从 2007 年开始撰写编程文章。迄今为止,我已撰写了 1400 多篇文章和 8 本电子书。我在教学编程方面拥有十多年的经验。

列出 所有 Perl 教程