Perl formatted (format) Report output implementation code _perl

Source: Internet
Author: User
Tags first row truncated


Perl has the best text-processing capabilities. This is what everyone knows. In Perl itself, there is a small function that no other software has, the Perl format. It is equivalent to simple command-line reports and chart output. This needs to be declared in the format. Create a file handle for the format. and then by @,^,<,> , | Such characters form what is called a "report" to output a chart. This allows you to specify the appearance of the trip. followed by a data item, and finally using write to display the formatted content.



Sample Example:
Output record format output is as defined below:
Format NAME =
Formlist
.



The first part, the statement. If you omit NAME, the above code defines the format output STDOUT.
In the second section, the format line formlist consists of some ordered rows, each of which is one of the following three types:
1. Comments are expressed in the first column.
2. A format line that defines the format of an output line, which refers to a character such as the @,^,<,>,| above
3. Parameter rows, data rows, used to insert values into the previous format line, are Perl variables
Part three, end "." to indicate.



Format line (graphic line) syntax:
Each replaced part of the format line begins with a @ or ^. These lines do not make any form of variable substitution.
The @ field (not to be confused with the array symbol @) is a normal field.



The >,| The length of the field follows a specified length after the format symbol @,^, >,| To define, while <,>,| Also represents, left-aligned, right-aligned, center-aligned. If the variable exceeds the defined length, it is truncated.



^ field, which is used for multiple lines of text block padding.


 code as follows:

$text = "AAA bbb CCC";
Format STDOUT =
A: ^<<<<
$text
Second: ^<<<<
$text
Third: ^<<<<
$text
.
Write

So it will output as follows:
First:aaa
second:bbb
Third:ccc





It breaks down the string $text by default, like split. Of course, it's the $: stored content as a separator.
# Another way to align right (behind @ or ^), specify a number field after these symbols. You can insert one in this area. To set the position of the decimal point. If the values of these ranges contain a newline character, only the text preceding the line break is printed. such as @.# #就是输出二位小数.
@* can be used to print multiple rows of values that are not truncated, that is, multiple rows with no formatted output.



Parameter rows (data rows)
Parameter rows specify that the order of the parameters must match the field order of the corresponding format row. Expressions with different parameters need to be separated by commas.
The data for a parameter can be either scalar data or a return value of the function.
All parameter expressions are evaluated in the list environment before the argument row is processed, so a single list expression produces multiple list elements.
By enclosing an expression in parentheses, you can extend the expression to multiple lines (therefore, the parentheses must be the first flag of the first row). This allows the values to be matched with the corresponding format fields for easy reading.
expressions, whitespace characters \n,\t, and \f are always interpreted as a single space



Format variables



$~ ($FORMAT _name) format name
$^ ($FORMAT _top_name) The name of the current header format is stored in the
$% ($FORMAT _page_number) the page number of the current output
$= ($FORMAT _lines_per_page) The number of rows per page
$| ($FORMAT _autoflush) whether to automatically flush output buffer storage
$^l ($FORMAT _formfeed) The string that needs to be printed before the header of each page (except the first page) is stored in the



Note: These variables are set based on file handles, so you need to select the file handles associated with a particular format to affect these format variables



Call output
Write
Finally, the write function is used to display the formatted text content.



Multi-page report
$% the current output page number
Number of rows in $= page
$-, how many more lines are left?



When using output, it is best to build the top output of a table. It looks a lot nicer.



Instance code:


 code 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 is as follows
Page 3
PASSWD File
Name Login Office Uid Gid Home
--------------------------------------------------------------------
sshd x 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




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.