Perl formatted output
Perl is a very powerful text processing language data.
Perl can be used to define a template format, and then use the write data output by the specified template.
Perl format defined syntax is as follows:
format FormatName = fieldline value_one, value_two, value_three fieldline value_one, value_two .
Analytical parameters:
- FormatName: format name.
- fieldline: a row format, the format used to define an output line, similar to the @, ^, <,>, | such characters.
- value_one, value_two /en/en/en: rows of data, used to insert values to the front row format, all perl variables.
- : End symbol.
The following is a simple instance is formatted:
#!/usr/bin/perl $text = "google w3big taobao"; format STDOUT = first: ^<<<<< # 左边对齐,字符长度为6 $text second: ^<<<<< # 左边对齐,字符长度为6 $text third: ^<<<< # 左边对齐,字符长度为5,taobao 最后一个 o 被截断 $text . write
Perform the above example output is:
first: google second: w3big third: taoba
Format row (row graphical) syntax
- Format lines @ or ^ at the beginning, these lines do not undergo any form of variable substitution.
- @ Field (not to be confused with the array marker @) is an ordinary field.
- @, <,>, ^ After the | length determines the length of the field if the variable exceeds the defined length, it will be truncated.
- <,>, | Also represent, left-aligned, right-aligned, center aligned.
- ^ Fields for multi-line text block filling.
Format Range
Range of formats, as follows:
format | Range Meaning |
---|---|
@ <<< | Left output |
@ >>> | Right-justified output |
@ ||| | Align output |
@ ##. ## | Fixed precision number |
@ * | Multiple lines of text |
The first character of each line is a range of filler character when using the @ character, not text formatting.
In the above table, in addition to multi-line @ * range, wide-field are equal to a specified number of characters in the character @, including, for example:
@###.##
It represents seven characters wide, after four before the decimal point, the decimal point two.
Examples are as follows:
#!/usr/bin/perl format EMPLOYEE = =================================== @<<<<<<<<<<<<<<<<<<<<<< @<< $name $age @#####.## $salary =================================== . select(STDOUT); $~ = EMPLOYEE; @n = ("Ali", "w3big", "Jaffer"); @a = (20,30, 40); @s = (2000.00, 2500.00, 4000.000); $i = 0; foreach (@n){ $name = $_; $age = $a[$i]; $salary = $s[$i++]; write; }
The above example output is:
=================================== Ali 20 2000.00 =================================== =================================== w3big 30 2500.00 =================================== =================================== Jaffer 40 4000.00 ===================================
Variable format
- $ ~ ($ FORMAT_NAME): Format Name $ ^ ($ FORMAT_TOP_NAME): The current format name is stored in the header
- $% ($ FORMAT_PAGE_NUMBER): the current output page number
- $ = ($ FORMAT_LINES_PER_PAGE): the number of lines on the page
- $ | ($ FORMAT_AUTOFLUSH): whether to automatically flush the output buffer memory
- $ ^ L ($ FORMAT_FORMFEED): on every page (except the first page) header required before the output string is stored in
The following is a simple to use $ instance - format:
#!/usr/bin/perl $~ = "MYFORMAT"; # 指定缺省文件变量下所使用的格式 write; # 输出 $~ 所指定的格式 format MYFORMAT = # 定义格式 MYFORMAT ================================= Text # 本教程 ================================= . write;
Perform the above example output is:
================================= Text # 本教程 ================================= ================================= Text # 本教程 =================================
If you do not specify the $ ~ under the case, the output format called STDOUT:
#!/usr/bin/perl write; # 不指定$~的情况下会寻找名为STDOUT的格式 format STDOUT = ~用~号指定的文字不会被输出 ---------------- STDOUT格式 ---------------- .
Perform the above example output is:
---------------- STDOUT格式 ----------------
The following example we add a report header information to demonstrate the $ ^ or $ FORMAT_TOP_NAME variables:
#!/usr/bin/perl format EMPLOYEE = =================================== @<<<<<<<<<<<<<<<<<<<<<< @<< $name $age @#####.## $salary =================================== . format EMPLOYEE_TOP = =================================== Name Age =================================== . select(STDOUT); $~ = EMPLOYEE; $^ = EMPLOYEE_TOP; @n = ("Ali", "w3big", "Jaffer"); @a = (20,30, 40); @s = (2000.00, 2500.00, 4000.000); $i = 0; foreach (@n){ $name = $_; $age = $a[$i]; $salary = $s[$i++]; write; }
The above example output is:
=================================== Name Age =================================== =================================== Ali 20 2000.00 =================================== =================================== w3big 30 2500.00 =================================== =================================== Jaffer 40 4000.00 ===================================
We can also use the $% or $ FORMAT_PAGE_NUMBER set pagination for the report:
#!/usr/bin/perl format EMPLOYEE = =================================== @<<<<<<<<<<<<<<<<<<<<<< @<< $name $age @#####.## $salary =================================== . # 添加分页 $% format EMPLOYEE_TOP = =================================== Name Age Page @< $% =================================== . select(STDOUT); $~ = EMPLOYEE; $^ = EMPLOYEE_TOP; @n = ("Ali", "w3big", "Jaffer"); @a = (20,30, 40); @s = (2000.00, 2500.00, 4000.000); $i = 0; foreach (@n){ $name = $_; $age = $a[$i]; $salary = $s[$i++]; write; }
The above example output is:
=================================== Name Age Page 1 =================================== =================================== Ali 20 2000.00 =================================== =================================== w3big 30 2500.00 =================================== =================================== Jaffer 40 4000.00 ===================================
Output to other file
By default, the function will write the results to the standard output file STDOUT, we can also make it outputs the result to any other file. The easiest way is to file variable as a parameter to write, such as:
write(MYFILE);
Write the code above to use the default print format output to a file called MYFILE MYFILE in.
But this can not be used to change the print format used by $ ~ variable. System variables $ ~ variable works only for the default file, we can change the default file variable $ ~ changes, then call write.
#!/usr/bin/perl if (open(MYFILE, ">tmp")) { $~ = "MYFORMAT"; write MYFILE; # 含文件变量的输出,此时会打印与变量同名的格式,即MYFILE。$~里指定的值被忽略。 format MYFILE = # 与文件变量同名 ================================= 输入到文件中 ================================= . close MYFILE; }
After the successful implementation, we can view the contents of tmp file, as follows:
$ cat tmp ================================= 输入到文件中 =================================
We can use the select to change the default file variable, which returns the current internal variable indicates the default file, so that we can create a subprogram, according to their own ideas output, without affecting the rest of the program.
#!/usr/bin/perl if (open(MYFILE, ">>tmp")) { select (MYFILE); # 使得默认文件变量的打印输出到MYFILE中 $~ = "OTHER"; write; # 默认文件变量,打印到select指定的文件中,必使用$~指定的格式 OTHER format OTHER = ================================= 使用定义的格式输入到文件中 ================================= . close MYFILE; }
After the successful implementation, we can view the contents of tmp file, as follows:
$ cat tmp ================================= 输入到文件中 ================================= ================================= 使用定义的格式输入到文件中 =================================