Perl formats the output

Perl is a very powerful text data processing language.

You can use format in Perl to define a template, and then use write to output data by the specified template.

Perl formats the definition syntax format as follows:

format FormatName =
fieldline
value_one, value_two, value_three
fieldline
value_one, value_two
.

Parameter resolution:

  • FormatName: Format the name.
  • Fieldline: A format line that defines the format of an output line, similar to the format of a | such characters.
  • value_one,value_two...... : Data rows, which are used to insert values into the previous format rows, are perl variables.
  • . : The end symbol.

Here's a simple example of formatting:

#!/usr/bin/perl

$text = "google youj taobao";
format STDOUT =
first: ^<<<<<  # 左边对齐,字符长度为6
    $text
second: ^<<<<< # 左边对齐,字符长度为6
    $text
third: ^<<<< # 左边对齐,字符长度为5,taobao 最后一个 o 被截断
    $text  
.
write

The output of the above instances is:

first: google
second: youj
third: taoba

Format row (graphic row) syntax

  • Format lines start with , or , and they do not replace variables of any kind.
  • The field (not to be confused with the array symbol s) is a normal field.
  • The post-, slt;, , and the | Length determines the length of the field, and if the variable exceeds the defined length, it will be truncated.
  • <, >,| Also indicated, left alignment, right alignment, center alignment.
  • The field is used for multi-line block fills of text.

The value domain format

The format of the value field, as shown in the following table:

Format The meaning of the value field
@<<< The output is aligned to the left
@>>> Align the output to the right
@||| Align the output in
@##.## Fixed precision numbers
@* Multiple lines of text

The first character of each value field is a line filler, which is not formatted with text when using the character.

In the table above, the width of the field is equal to the number of characters it specifies to include characters, for example:

@###.##

Represents seven characters wide, the first four of the dots, and the last two of the dots.

Here's an example:

#!/usr/bin/perl

format EMPLOYEE =
===================================
@<<<<<<<<<<<<<<<<<<<<<< @<< 
$name $age
@#####.##
$salary
===================================
.

select(STDOUT);
$~ = EMPLOYEE;

@n = ("Ali", "W3CSchool", "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 output of the above examples is:

===================================
Ali                     20
  2000.00
===================================
===================================
W3CSchool                  30
  2500.00
===================================
===================================
Jaffer                  40
  4000.00
===================================

Format variable

  • $-($FORMAT-NAME): Format name $-($FORMAT-TOP-NAME): The current header format name is stored
  • $% ($FORMAT-PAGE-NUMBER): The page number of the current output
  • $=($FORMAT-LINES-PER-PAGE): Number of rows per page
  • $| ($FORMAT AUTOFLUSH): Whether to automatically flush the output buffer store
  • $-L ($FORMAT-FORMFEED): Strings that need to be exported before each page (except the first page) header is stored

Here's a simple example of using $-formatted:

#!/usr/bin/perl

$~ = "MYFORMAT"; # 指定缺省文件变量下所使用的格式
write;           # 输出 $~ 所指定的格式

format MYFORMAT = # 定义格式 MYFORMAT 
=================================
      Text # W3Cschool教程
=================================
.
write; 

The output of the above instances is:

=================================
      Text # W3Cschool教程
=================================
=================================
      Text # W3Cschool教程
=================================

If you don$t specify $, a format called STDOUT is output:

#!/usr/bin/perl

write;         # 不指定$~的情况下会寻找名为STDOUT的格式

format STDOUT =
~用~号指定的文字不会被输出
----------------
  STDOUT格式
----------------
.

The output of the above instances is:

----------------
  STDOUT格式
----------------

Here's an example of how we can demonstrate the use of $ $- or $FORMAT-TOP-NAME variable by adding report header information:

#!/usr/bin/perl

format EMPLOYEE =
===================================
@<<<<<<<<<<<<<<<<<<<<<< @<< 
$name $age
@#####.##
$salary
===================================
.

format EMPLOYEE_TOP =
===================================
Name                    Age
===================================
.

select(STDOUT);
$~ = EMPLOYEE;
$^ = EMPLOYEE_TOP;

@n = ("Ali", "W3CSchool", "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 output of the above examples is:

===================================
Name                    Age
===================================
===================================
Ali                     20
  2000.00
===================================
===================================
W3CSchool                  30
  2500.00
===================================
===================================
Jaffer                  40
  4000.00
===================================

We can also set up peddles $FORMAT report using $% or using the 2000s:PAGE_NUMBER:

#!/usr/bin/perl

format EMPLOYEE =
===================================
@<<<<<<<<<<<<<<<<<<<<<< @<< 
$name $age
@#####.##
$salary
===================================
.

# 添加分页 $% 
format EMPLOYEE_TOP =
===================================
Name                    Age Page @<
                                 $%
=================================== 
.

select(STDOUT);
$~ = EMPLOYEE;
$^ = EMPLOYEE_TOP;

@n = ("Ali", "W3CSchool", "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 output of the above examples is:

===================================
Name                    Age Page 1
===================================
===================================
Ali                     20
  2000.00
===================================
===================================
W3CSchool                  30
  2500.00
===================================
===================================
Jaffer                  40
  4000.00
===================================

Output to another file

By default, the function write outputs the results to the standard output file STDOUT, and we can also make it output to any other file. The easiest way is to pass the file variables as parameters to the white, such as:

write(MYFILE);

The above code, write, is output to the file MYFILE in the default print format called MYFILE.

However, you can't change the print format you're using with the $-variable. The system variable $-only works on the default file variable, and we can change the default file variable, change the $-,, and then call white.

#!/usr/bin/perl

if (open(MYFILE, ">tmp")) {
$~ = "MYFORMAT";
write MYFILE; # 含文件变量的输出,此时会打印与变量同名的格式,即MYFILE。$~里指定的值被忽略。

format MYFILE = # 与文件变量同名 
=================================
      输入到文件中
=================================
.
close MYFILE;
}

After a successful execution, we can look at the contents of the tmp file as follows:

$ cat tmp 
=================================
      输入到文件中
=================================

When we can use select to change the default file variable, it returns the internal represent of the current default file variable, so that we can create sub-programs that output as we see it, 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 a successful execution, we can look at the contents of the tmp file as follows:

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