Perl Formatting Output
Perl is a very powerful text data processing language.
In Perl, you can use format to define a template, and then use write to output data according to the specified template.
The syntax for Perl format definition is as follows:
format FormatName = fieldline value_one, value_two, value_three fieldline value_one, value_two .
Parameter explanation:
- FormatNameFormat name.
- fieldlineA format line, used to define the format of an output line, similar to characters such as @, ^, <, >, |.
- value_one,value_two……Data lines, used to insert values into the preceding format lines; these are all Perl variables.
- .Termination symbol.
The following is a simple formatting example:
Example
Executing the above example produces the following output:
first: google second: example third: taoba
Format Line (Picture Line) Syntax
- Format lines start with @ or ^, and these lines do not perform any form of variable substitution.
- @ field (not to be confused with the array symbol @) is an ordinary field.
- The <, >, | after @, ^ determine the length of the field; if the variable exceeds the defined length, it will be truncated.
- <, >, | also indicate left alignment, right alignment, and center alignment respectively.
- ^ fields are used for filling multi-line text blocks.
Field Formats
The field formats are shown in the following table:
| Format | Field Meaning |
|---|---|
| @<<< | Left-aligned output |
| @>>> | Right-aligned output |
| @||| | Center-aligned output |
| @##.## | Fixed precision number |
| @* | Multi-line text |
The first character of each field is the line-fill character. When the @ character is used, no text formatting is performed.
In the above table, except for the multi-line field @*, the field width is equal to the number of specified characters including the @ character, for example:
@###.##
Indicates seven characters wide, four before the decimal point, and two after the decimal point.
The example is as follows:
Example
Executing the above example produces the following output:
=================================== Ali 20 2000.00 =================================== =================================== Example 30 2500.00 =================================== =================================== Jaffer 40 4000.00 ===================================
Format Variables
- $~ ($FORMAT_NAME): format name $^ ($FORMAT_TOP_NAME): the current header format name is stored in
- $% ($FORMAT_PAGE_NUMBER): the current output page number
- $= ($FORMAT_LINES_PER_PAGE): the number of lines per page
- $| ($FORMAT_AUTOFLUSH): whether to automatically flush the output buffer storage
- $^L ($FORMAT_FORMFEED): the string that needs to be output before the header on each page (except the first page) is stored in
The following is a simple example of formatting using $~:
Example
Executing the above example produces the following output:
=================================
Text # Example
=================================
=================================
Text # Example
=================================
If $~ is not specified, a format named STDOUT will be output:
Example
Executing the above example produces the following output:
---------------- STDOUT格式 ----------------
In the following example, we demonstrate the use of the $^ or $FORMAT_TOP_NAME variables by adding report header information:
Example
Executing the above example produces the following output:
=================================== Name Age =================================== =================================== Ali 20 2000.00 =================================== =================================== Example 30 2500.00 =================================== =================================== Jaffer 40 4000.00 ===================================
We can also use $% or $FORMAT_PAGE_NUMBER to set pagination for the report:
Example
Executing the above example produces the following output:
=================================== Name Age Page 1 =================================== =================================== Ali 20 2000.00 =================================== =================================== Example 30 2500.00 =================================== =================================== Jaffer 40 4000.00 ===================================
Output to Other Files
By default, the write function outputs results to the standard output file STDOUT. We can also make it output results to any other file. The simplest way is to pass the file variable as a parameter to write, for example:
write(MYFILE);
In the above code, write uses the print format named MYFILE by default to output to the file MYFILE.
However, in this way you cannot use the $~ variable to change the print format used. The system variable $~ only works on the default file variable. We can change the default file variable, change $~, and then call write.
Example
After successful execution, we can view the contents of the tmp file, as shown below:
$ cat tmp
=================================
输入到文件中
=================================
When we use select to change the default file variable, it returns the internal representation of the current default file variable. In this way, we can create subroutines to output according to our own ideas without affecting other parts of the program.
Example
After successful execution, we can view the contents of the tmp file, as shown below:
$ cat tmp
=================================
输入到文件中
=================================
=================================
使用定义的格式输入到文件中
=================================
Other Extensions