Perl 简明教程
Perl - Formats
Perl 使用称为“格式”的写模板来输出报告。要使用 Perl 的格式特征,你必须先定义一个格式,然后才能使用该格式来编写格式化数据。
Perl uses a writing template called a 'format' to output reports. To use the format feature of Perl, you have to define a format first and then you can use that format to write formatted data.
Define a Format
以下是定义 Perl 格式的语法 -
Following is the syntax to define a Perl format −
format FormatName =
fieldline
value_one, value_two, value_three
fieldline
value_one, value_two
.
此处, FormatName 表示格式的名称。 fieldline 是数据应如何格式化的特定方式。值行表示将输入到字段行中的值。你以一个句号结束格式。
Here FormatName represents the name of the format. The fieldline is the specific way, the data should be formatted. The values lines represent the values that will be entered into the field line. You end the format with a single period.
接下来, fieldline 可以包含任何文本或字段持有器。字段持有器保留空间以容纳稍后将放入其中的数据。字段持有器的格式为 -
Next fieldline can contain any text or fieldholders. The fieldholders hold space for data that will be placed there at a later date. A fieldholder has the format −
@<<<<
该字段持有器左对齐,具有 5 个字段空间。你必须计算 @ 符号和 < 符号来得知字段中的空格数。其他字段持有器包括 -
This fieldholder is left-justified, with a field space of 5. You must count the @ sign and the < signs to know the number of spaces in the field. Other field holders include −
@>>>> right-justified
@|||| centered
@####.## numeric field holder
@* multiline field holder
一个示例格式为 -
An example format would be −
format EMPLOYEE =
===================================
@<<<<<<<<<<<<<<<<<<<<<< @<<
$name $age
@#####.##
$salary
===================================
.
在此示例中,$name 将被写成 22 个字符空间内的左对齐,之后 age 将被写在两个空间中。
In this example, $name would be written as left justify within 22 character spaces and after that age will be written in two spaces.
Using the Format
为了调用此格式声明,我们将使用 write 关键字 -
In order to invoke this format declaration, we would use the write keyword −
write EMPLOYEE;
问题在于,格式名称通常是打开文件句柄的名称,而写语句会将输出发送到此文件句柄。由于我们希望将数据发送给 STDOUT,因此我们必须将 EMPLOYEE 与 STDOUT 文件句柄关联起来。但是,首先,我们必须确保 STDOUT 是我们选定的文件句柄,方法是使用 select() 函数。
The problem is that the format name is usually the name of an open file handle, and the write statement will send the output to this file handle. As we want the data sent to the STDOUT, we must associate EMPLOYEE with the STDOUT filehandle. First, however, we must make sure that that STDOUT is our selected file handle, using the select() function.
select(STDOUT);
然后,我们将通过使用特殊变量 $~ 或 $FORMAT_NAME 将新的格式名称与 STDOUT 相关联,如下所示: -
We would then associate EMPLOYEE with STDOUT by setting the new format name with STDOUT, using the special variable $~ or $FORMAT_NAME as follows −
$~ = "EMPLOYEE";
当我们现在执行写操作时,数据将发送至 STDOUT。记住:如果你要将报告写入 STDOUT 以外的任何其他文件句柄,则可以使用 select() 函数来选择该文件句柄,而其余逻辑将保持不变。
When we now do a write(), the data would be sent to STDOUT. Remember: if you are going to write your report in any other file handle instead of STDOUT then you can use select() function to select that file handle and rest of the logic will remain the same.
我们来看以下示例。在此,我们硬编码了值,仅用于演示用法。在实际用法中,你将从文件或数据库中读取值以生成实际报告,并且可能需要将最终报告再次写入文件。
Let’s take the following example. Here we have hard coded values just for showing the usage. In actual usage you will read values from a file or database to generate actual reports and you may need to write final report again into a file.
#!/usr/bin/perl
format EMPLOYEE =
===================================
@<<<<<<<<<<<<<<<<<<<<<< @<<
$name $age
@#####.##
$salary
===================================
.
select(STDOUT);
$~ = EMPLOYEE;
@n = ("Ali", "Raza", "Jaffer");
@a = (20,30, 40);
@s = (2000.00, 2500.00, 4000.000);
$i = 0;
foreach (@n) {
$name = $_;
$age = $a[$i];
$salary = $s[$i++];
write;
}
执行后,将生成以下结果 -
When executed, this will produce the following result −
===================================
Ali 20
2000.00
===================================
===================================
Raza 30
2500.00
===================================
===================================
Jaffer 40
4000.00
===================================
Define a Report Header
一切看起来都很好。但是,你可能希望向你的报告添加页眉。此页眉将打印在每页顶部。要做到这一点非常简单。除了定义模板外,你还必须定义页眉并将其分配给 $^ 或 $FORMAT_TOP_NAME 变量 -
Everything looks fine. But you would be interested in adding a header to your report. This header will be printed on top of each page. It is very simple to do this. Apart from defining a template you would have to define a header and assign it to $^ or $FORMAT_TOP_NAME variable −
#!/usr/bin/perl
format EMPLOYEE =
===================================
@<<<<<<<<<<<<<<<<<<<<<< @<<
$name $age
@#####.##
$salary
===================================
.
format EMPLOYEE_TOP =
===================================
Name Age
===================================
.
select(STDOUT);
$~ = EMPLOYEE;
$^ = EMPLOYEE_TOP;
@n = ("Ali", "Raza", "Jaffer");
@a = (20,30, 40);
@s = (2000.00, 2500.00, 4000.000);
$i = 0;
foreach (@n) {
$name = $_;
$age = $a[$i];
$salary = $s[$i++];
write;
}
现在,你的报告将如下所示 -
Now your report will look like −
===================================
Name Age
===================================
===================================
Ali 20
2000.00
===================================
===================================
Raza 30
2500.00
===================================
===================================
Jaffer 40
4000.00
===================================
Define a Pagination
如果你的报告超过一页,该怎么办?你有个解决办法,只需按照以下步骤使用 $% 或 $FORMAT_PAGE_NUMBER 变量加上标题 −
What about if your report is taking more than one page? You have a solution for that, simply use $% or $FORMAT_PAGE_NUMBER vairable along with header as follows −
format EMPLOYEE_TOP =
===================================
Name Age Page @<
$%
===================================
.
现在,你的输出将如下所示 −
Now your output will look like as follows −
===================================
Name Age Page 1
===================================
===================================
Ali 20
2000.00
===================================
===================================
Raza 30
2500.00
===================================
===================================
Jaffer 40
4000.00
===================================
Number of Lines on a Page
你可以使用特殊变量 $= (或 $FORMAT_LINES_PER_PAGE) 设置每页的行数,默认为 60。
You can set the number of lines per page using special variable $= ( or $FORMAT_LINES_PER_PAGE ), By default $= will be 60.
Define a Report Footer
而 $^ 或 $FORMAT_TOP_NAME 包含当前标题格式的名称,没有相应的机制可以自动为页脚执行相同操作。如果你有一个固定大小的页脚,可以通过在每次 write() 之前检查变量 $- 或 $FORMAT_LINES_LEFT 来获取页脚,并在需要时使用另一个定义如下格式自己打印页脚 −
While $^ or $FORMAT_TOP_NAME contains the name of the current header format, there is no corresponding mechanism to automatically do the same thing for a footer. If you have a fixed-size footer, you can get footers by checking variable $- or $FORMAT_LINES_LEFT before each write() and print the footer yourself if necessary using another format defined as follows −
format EMPLOYEE_BOTTOM =
End of Page @<
$%
.
有关与格式设置相关的变量的完整集合,请参考 Perl Special Variables 部分。
For a complete set of variables related to formating, please refer to the Perl Special Variables section.