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 Perl formatting definition syntax is as follows:
format FormatName =fieldlinevalue_one, value_two, value_threefieldlinevalue_one, value_two.
Parameter analysis:
FormatName : Formatted name.
fieldline : A format line used to define the format of an output line, with characters like @,^,,|.
value_one, value_two... : Data lines, used to insert values into the previous format lines, are all perl variables.
. : end symbol.
The following is a simple formatting example:
The output result of executing the above example is:
first: googlesecond: coderctothird: taoba
Format lines begin with @ or ^, and no variable substitution is performed on these lines.
@ fields (not to be confused with the array symbol @) are ordinary fields.
The length of <, >,| after @,^ determines the length of the field. If the variable exceeds the defined length, it will be truncated.
<, >,| also represent left alignment, right alignment, and center alignment respectively.
The ^ field is used for multi-line text block padding.
The format of the value range is as shown in the following table:
| Format | Value range meaning |
|---|---|
| @<<< | left aligned output |
| @>>> | Right justified output |
| @||| | Center aligned output |
| @##.## | fixed precision number |
| @* | multiline text |
The first character of each value field is the line filler character. When the @ character is used, no text formatting is done.
In the above table, except for the multi-line value field @*, the field width is equal to the specified number of characters including the character @, for example:
@###.##
It means seven characters wide, four before the decimal point and two after the decimal point.
Examples are as follows:
The output result of the above example is:
===================================Ali 20 2000.00============ ================================================== ========Codercto 30 2500.00================================================== =====================Jaffer 40 4000.00========================== =========
$~ ($FORMAT_NAME): Format name $^ ($FORMAT_TOP_NAME): The current header format name is stored in
$% ($FORMAT_PAGE_NUMBER): current output page number
$= ($FORMAT_LINES_PER_PAGE) : Number of lines in each page
$| ($FORMAT_AUTOFLUSH): Whether to automatically refresh the output buffer storage
$^L ($FORMAT_FORMFEED): The string that needs to be output before the header of each page (except the first page) is stored in
The following is an example of simple formatting using $~:
The output result of executing the above example is:
================================= Text # Coder Tutorial============ ================================================== ==== Text # Coder Tutorial==================================
If $~ is not specified, a format named STDOUT will be output:
The output result of executing the above example is:
----------------STDOUT format----------------
In the following example, we demonstrate the use of $^ or $FORMAT_TOP_NAME variables by adding report header information:
The output result of the above example is:
===================================Name Age============== ================================================== =======Ali 20 2000.00========================================== ==============================Codercto 30 2500.00================================================== =====================Jaffer 40 4000.00========================== =========
We can also use $% or $FORMAT_PAGE_NUMBER to set pagination for the report:
The output result of the above example is:
===================================Name Age Page 1============ ================================================== =========Ali 20 2000.00======================================== ================================Codercto 30 2500.00================================================== =====================Jaffer 40 4000.00========================== =========
By default, the write function outputs the results to the standard output file STDOUT, but we can also make it output the results to any other file. The simplest way is to pass the file variable as a parameter to write, such as:
write(MYFILE);
The above code write outputs to the file MYFILE using the default printing format named MYFILE.
But then you cannot use the $~ variable to change the printing format used. The system variable $~ only affects the default file variable. We can change the default file variable, change $~, and then call write.
After successful execution, we can view the contents of the tmp file as follows:
$ cat tmp ================================= Input into file========== =======================
When we can use select to change the default file variable, it returns the internal representation of the current default file variable, so that we can create subroutines and output according to our own ideas without affecting other parts of the program.
After successful execution, we can view the contents of the tmp file as follows:
$ cat tmp ================================= Input into file========== ================================================== ====== Input into file using defined format ===================================