Perl has the best text data processing capabilities. this is what everyone knows. in perl, some software does not have small functions, namely the Perl format. it is equivalent to a simple command line report and chart output. the format must be declared first. create a file handle in the format. then, the @, ^, <,>, | character form the so-called "report" to output the chart. in this way, you can specify the travel appearance. data items are followed, and write is used to display formatted content.
Example:
Output record format output is defined as below:
Format NAME =
FORMLIST
.
The first part declares. If the NAME is omitted above, the above Code will define the format to output STDOUT.
In the second part, the format row FORMLIST consists of ordered rows. Each row is one of the following three types:
1. annotation, expressed in column.
2. A format row is used to define the format of an output row. It refers to the preceding description of @, ^, <,>, |.
3. Parameter Line, data line, used to insert values in the format line forward, all are perl Variables
The third part ends.
Format line (graphic line) Syntax:
Each replaced part of the format line starts with @ or ^. These rows are not replaced by any form of variables.
@ Field (do not confuse with array symbol @) is a common field.
<,>, | The length of a field is defined by the format symbol @, ^ followed by a specific length <,>, |, and <,>, |, respectively, align left, right, and center. If the variable exceeds the defined length, it is truncated.
^ Field, used to fill in text blocks of multiple lines.
Copy codeThe Code is as follows: $ text = "aaa bbb ccc ";
Format STDOUT =
First: ^ <
$ Text
Second: ^ <
$ Text
Third: ^ <
$ Text
.
Write
In this way, the output is as follows:
First: aaa
Second: bbb
Third: ccc
By default, it will break down the string $ text, such as split. Of course, it is a separator for the $: saved content.
# (After @ or ^) another method of right alignment is to specify a numeric field after these symbols. You can insert a. In this area to specify the decimal point. If the values in these regions contain a line break, only the text before the line break is output. For example, @. # outputs two decimal places.
@ * Can be used to print the value of multiple rows without truncation, that is, the output of multiple rows without formatting.
Parameter row (Data row)
The order of the specified parameter in the parameter line must be the same as that in the corresponding format line. Expressions of different parameters must be separated by commas.
The parameter data can be scalar data or the return value of the function.
All parameter expressions before the Parameter Line is processed are evaluated in the list environment. Therefore, a single list expression generates multiple list elements.
You can enclose an expression by using a circular arc to extend the expression to multiple rows (therefore, the Circular Arc must be the first sign of the first row ). In this way, the value can be matched with the corresponding format field for easy reading.
In the expression, the white space characters \ n, \ t, and \ f are always interpreted as a single space.
Format variable
$ ~ Name in ($ FORMAT_NAME) Format
$ ^ ($ FORMAT_TOP_NAME) The current Header Format name is stored in
$ % ($ FORMAT_PAGE_NUMBER) current output page number
$ = ($ FORMAT_LINES_PER_PAGE) number of rows per page
$ | ($ FORMAT_AUTOFLUSH) Whether to automatically refresh the output buffer storage
$ ^ L ($ FORMAT_FORMFEED) strings to be output before each page (except the first page) header are stored in
Note: These variables are based on the file handle. Therefore, you need to select the file handle associated with a specific format to affect these format variables.
Call output
Write
Finally, you can directly use the write function to display formatted text content.
Multi-page reports
$ % Current output page number
$ = Number of rows on the page
$-Remaining number of rows
When using the output, it is best to create a top output of the table. This looks a lot better.
Instance code:
Copy codeThe Code is as follows :#! /Usr/bin/perl
Use strict;
Use warnings;
My ($ name, $ login, $ office, $ uid, $ gid, $ home );
Open FD, "</etc/passwd" or die "$? ";
While (<FD> ){
($ Name, $ login, $ office, $ uid, $ gid, $ home) = split (":", $ _);
$ ^ = 'Stdout _ top ';
$ ~ = 'Stdout ';
$ = 39;
Write;
}
Format STDOUT_TOP =
@ >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>
"Page $ %"
Passwd File
Name Login Office Uid Gid Home
------------------------------------------------------------------
.
Format STDOUT =
@ <@ | <@> @ >>>>>@ <
$ Name, $ login, $ office, $ uid, $ gid, $ home
.
Output:
Page 3
Passwd File
Name Login Office Uid Gid Home
--------------------------------------------------------------------
Sshd x 115 65534/var/run/sshd
Vde2-net x 116 128/var/run/vde2
Mysql x 117 129 MySQL/var/lib/mysql
Oracle x 1001 1001/usr/lib/oracle/xe
Tomcat6 x 118 130/usr/share/tomcat6