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.
The syntax format for Perl format definitions is as follows:
format FormatName =
fieldline
value_one, value_two, value_three
fieldline
value_one, value_two
.
Parameter analysis:
- FormatName : the format name.
- fieldline : a format line, used to define the format of an output line, similar to @,^,<,>,| such characters.
- value_one,value_two…… : a data line, used to insert values into the preceding format lines; all are Perl variables.
- . : the ending symbol.
The following is a simple formatting example:
Example
#!/usr/bin/perl
$text
=
"
google runoob taobao
"
;
format
STDOUT
=
first
: ^<<<<<
Left-aligned, with a character length of 6
$text
second
: ^<<<<<
Left-aligned, with a character length of 6
$text
third
: ^<<<<
Left-aligned, with a character length of 5; the last o of taobao is truncated
$text
.
write
The output of running the above example is:
first: google
second: runoob
third: taoba
Syntax of format lines (picture lines)
- Format lines begin with @ or ^, and no form of variable substitution is performed on these lines.
- The @ field (not to be confused with the array symbol @) is an ordinary field.
- after @,^ the <, >,| the length determines the field length; if a variable exceeds the defined length, it will be truncated.
- <, >,| also indicate left alignment, right alignment, and centered alignment, respectively.
- The ^ field is used for filling multi-line text blocks.
Field formats
The formats of fields are shown in the following table:
| Format | Field meaning |
|---|---|
| @<<< | Left-aligned output |
| @>>> | Right-aligned output |
| @||| | Centered output |
| @##.## | Fixed-precision number |
| @* | Multi-line Text |
The first character of each field is the line filler; when the @ character is used, no text formatting is performed.
In the above table, except for the multi-line field @*, the field width equals the specified number of characters including the @ character, for example:
@###.##
means seven characters wide, four before the decimal point and two after it.
The example is as follows:
Example
#!/usr/bin/perl
format
EMPLOYEE
= =================================== @<<<<<<<<<<<<<<<<<<<<<< @<<
$name
,
$age
@
#####.##
$salary
=================================== .
select
(
STDOUT
)
; $~ =
EMPLOYEE
;
@n
=
(
"
Ali
"
,
"
Runoob
"
,
"
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 example is:
===================================
Ali 20
2000.00
===================================
===================================
Runoob 30
2500.00
===================================
===================================
Jaffer 40
4000.00
===================================
Format variables
- $~ ($FORMAT_NAME): the format name. $^ ($FORMAT_TOP_NAME): the current page header format name is stored in
- $% ($FORMAT_PAGE_NUMBER): the page number currently being output
- $= ($FORMAT_LINES_PER_PAGE): the number of lines per page
- $| ($FORMAT_AUTOFLUSH): whether the output buffer is automatically flushed
- $^L ($FORMAT_FORMFEED): the string to be output before the header of each page (except the first) is stored in
The following is a simple example of formatting using $~:
Example
#!/usr/bin/perl
$~ =
"
MYFORMAT
"
;
Specify the format used by the default file variable
write
;
Output the format specified by $~
format
MYFORMAT
=
Define the format MYFORMAT
=================================
Text
Runoob Tutorial
================================= .
write
;
The output of running the above example is:
=================================
Text # Runoob Tutorial
=================================
=================================
Text # Runoob Tutorial
=================================
If $~ is not specified, the format named STDOUT will be output:
Example
#!/usr/bin/perl
write
;
When $~ is not specified, a format named STDOUT will be sought
format
STDOUT
= ~ the text specified by the ~ sign will not be output ----------------
STDOUT
Format ---------------- .
The output of running the above example is:
----------------
STDOUT格式
----------------
In the following example, we demonstrate the use of the $^ or $FORMAT_TOP_NAME variable 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
"
,
"
Runoob
"
,
"
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 example is:
===================================
Name Age
===================================
===================================
Ali 20
2000.00
===================================
===================================
Runoob 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
"
,
"
Runoob
"
,
"
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 example is:
===================================
Name Age Page 1
===================================
===================================
Ali 20
2000.00
===================================
===================================
Runoob 30
2500.00
===================================
===================================
Jaffer 40
4000.00
===================================
Output to other files
By default, the write function outputs the result to the standard output file STDOUT; we can also make it output the result to any other file. The simplest way is to pass the file variable as an argument to write, for example:
write(MYFILE);
In the above code, write outputs to the file MYFILE using the print format also named MYFILE by default.
But 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
;
For output with a file variable, the format with the same name as the variable, namely MYFILE, is printed at this time. The value specified in $~ is ignored.
format
MYFILE
=
Same name as the file variable
================================= Input to 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. This allows us to create subroutines that output as we wish without affecting other parts of the program.
Example
#!/usr/bin/perl
if
(
open
(
MYFILE
,
"
tmp
"
)
)
{
select
(
MYFILE
)
;
Make the default file variable print output to MYFILE
$~ =
"
OTHER
"
;
write
;
Default file variable, printing to the file specified by select, must use the format specified by $~ OTHER
format
OTHER
= ================================= Input to the file using the defined format ================================= .
close
MYFILE
;
}
After successful execution, we can view the contents of the tmp file, as shown below:
$ cat tmp
=================================
输入到文件中
=================================
=================================
使用定义的格式输入到文件中
=================================