Perl formatted 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.

Perl formatting definition syntax is as follows:

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

Parameter analysis:

  • FormatName: format name.

  • fieldline: A format line, used to define the format of an output line, with characters like @,^,<,>,|.

  • value_one,value_two......: Data row, used to insert values ​​into the previous format row, all are perl variables.

  • .: end symbol.

The following is a simple formatting example:

#!/usr/bin/perl

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

The output result of executing the above example is:

first: google
second: php
third: taoba

Format line (graphic line) syntax

  • Format lines start with @ or ^, these lines do not perform any form of variable substitution.

  • @ 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 filling.

Value field format

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

##@ ##.##Fixed precision number @*Multi-line text##

The first character of each value field is the line filler character. When the @ character is used, no text formatting is performed.

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:

@###.##

means seven characters wide , four decimal points before and two decimal points after.

The example is as follows:

#!/usr/bin/perl

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

select(STDOUT);
$~ = EMPLOYEE;

@n = ("Ali", "php", "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 result of the above example is:

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

Format variable

  • ##$~ ($ 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): 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 a simple way to use $~ Formatting example:

#!/usr/bin/perl

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

format MYFORMAT = # 定义格式 MYFORMAT 
=================================
      Text # php中文网
=================================
.
write;

The output result of executing the above example is:

=================================
      Text # php中文网
=================================
=================================
      Text # php中文网
=================================

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

#!/usr/bin/perl

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

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

The output result of executing the above example is:

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

In the following example, we demonstrate the use of $^ or $FORMAT_TOP_NAME variables 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", "php", "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 result of the above example is:

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

We can also use $% or $FORMAT_PAGE_NUMBER to set paging for the report:

#!/usr/bin/perl

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

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

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

@n = ("Ali", "php", "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 result of the above example is:

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


Output to other files

By default, the write function outputs the results to the standard output file STDOUT. We can also make it output the results to any other file. The simplest method 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 this way 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.

#!/usr/bin/perl

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

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

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

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

When we can 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 and output them according to our own ideas without affecting other parts of the program.

#!/usr/bin/perl

if (open(MYFILE, ">>tmp")) {
select (MYFILE); # 使得默认文件变量的打印输出到MYFILE中
$~ = "OTHER";
write;           # 默认文件变量,打印到select指定的文件中,必使用$~指定的格式 OTHER

format OTHER =
=================================
  使用定义的格式输入到文件中
=================================
. 
close MYFILE;
}

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

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

Format Value range meaning
@<<<Left-aligned output
@>> ;>Right-aligned output
@|||Mid-aligned output