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

#!/usr/bin/perl $text = "google example taobao"; format STDOUT = first: ^<<<<< # Left-aligned, character length is 6 $text second: ^<<<<< # Left-aligned, character length is 6 $text third: ^<<<< # Left-aligned, character length is 5, the last o of taobao is truncated $text . write

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

#!/usr/bin/perl format EMPLOYEE = =================================== @<<<<<<<<<<<<<<<<<<<<<< @<< $name, $age @#####.## $salary =================================== . select(STDOUT); $~ = EMPLOYEE; @n = ("Ali", "Example", "Jaffer"); @a = (20,30, 40); @s = (2000.00, 2500.00, 4000.000); $i = 0; foreach (@n){ $name = $_; $age = $a[$i]; $salary = $s[$i++]; write; }

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

#!/usr/bin/perl $~ = "MYFORMAT"; # Specify the format used under the default file variable write; # Output the format specified by $~ format MYFORMAT = # Define format MYFORMAT ================================= Text # Example Tutorial ================================= . write;

Executing the above example produces the following output:

=================================
      Text # Example
=================================
=================================
      Text # Example
=================================

If $~ is not specified, a format named STDOUT will be output:

Example

#!/usr/bin/perl write; # If $~ is not specified, a format named STDOUT is searched for format STDOUT= ~Text specified with ~ will not be output ----------------STDOUTFormat ---------------- .

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

#!/usr/bin/perl format EMPLOYEE = =================================== @<<<<<<<<<<<<<<<<<<<<<< @<< $name, $age @#####.## $salary =================================== . format EMPLOYEE_TOP = =================================== Name Age =================================== . select(STDOUT); $~ = EMPLOYEE; $^ = EMPLOYEE_TOP; @n = ("Ali", "Example", "Jaffer"); @a = (20,30, 40); @s = (2000.00, 2500.00, 4000.000); $i = 0; foreach (@n){ $name = $_; $age = $a[$i]; $salary = $s[$i++]; write; }

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

#!/usr/bin/perl format EMPLOYEE = =================================== @<<<<<<<<<<<<<<<<<<<<<< @<< $name, $age @#####.## $salary =================================== . # Add pagination $% format EMPLOYEE_TOP = =================================== Name Age Page @< $% =================================== . select(STDOUT); $~ = EMPLOYEE; $^ = EMPLOYEE_TOP; @n = ("Ali", "Example", "Jaffer"); @a = (20,30, 40); @s = (2000.00, 2500.00, 4000.000); $i = 0; foreach (@n){ $name = $_; $age = $a[$i]; $salary = $s[$i++]; write; }

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

#!/usr/bin/perl if (open(MYFILE, ">tmp")) { $~ = "MYFORMAT"; write MYFILE; # Output including a file variable. At this time, the format with the same name as the variable, i.e., MYFILE, is printed. The value specified in $~ is ignored. format MYFILE = # Same name as the file variable= ================================ Write into the file ================================= .close MYFILE; }

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

#!/usr/bin/perl if (open(MYFILE, ">>tmp")) { select (MYFILE); # Make the print output of the default file variable go to MYFILE $~ = "OTHER"; write; # Default file variable, prints to the file specified by select, must use the format OTHER specified by $~ format OTHER= ================================= Use the defined format to write into the file ================================= .close MYFILE; }

After successful execution, we can view the contents of the tmp file, as shown below:

$ cat tmp 
=================================
      输入到文件中
=================================
=================================
  使用定义的格式输入到文件中
=================================
Other Extensions