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 defining a Perl format is as follows:
format FormatName = fieldline value_one, value_two, value_three fieldline value_one, value_two .
Parameter analysis:
- FormatName: The name of the format.
- fieldline: A format line that defines the format of an output line, using characters like
@,^,<,>,|. - value_one, value_two, ...: Data lines used to insert values into the preceding format line; these are Perl variables.
- .: The termination symbol.
Here is a simple formatting example:
Example
#!/usr/bin/perl $text = "google taobao"; format STDOUT = first: ^<<<<< $text second: ^<<<<<< $text third: ^<<<<< $text . write
Executing the above example produces the following output:
first: google second: third: taobao
Format Line (Picture Line) Syntax
- Format lines start with
@or^. These lines do not perform any form of variable substitution. - The
@field (not to be confused with the array symbol@) is a regular field. - The length following
@or^(using<,>,|) determines the field length. 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.
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 filler. When using the @ character, no text formatting is applied.
In the table above, except for the multi-line field @*, the field width is equal to the number of characters specified, including the @ character. For example:
@###.##
Represents a width of seven characters: four before the decimal point and two after.
Example:
Example
#!/usr/bin/perl
format EMPLOYEE =
===================================
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<
$name, $age
@$salary
===================================
.
select(STDOUT);
$~ = EMPLOYEE;
@n = ("Ali", "", "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 =================================== 30 2500.00 =================================== Jaffer 40 4000.00 ===================================
Format Variables
$~($FORMAT_NAME): The format name.$^($FORMAT_TOP_NAME): The current header format name is stored here.$%($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.$^L($FORMAT_FORMFEED): The string to output before the header of each page (except the first page) is stored here.
Here is a simple example using $~ for formatting:
Example
#!/usr/bin/perl $~ = "MYFORMAT"; write; format MYFORMAT = ================================= Text ================================= . write;
Executing the above example produces the following output:
================================= Text # ================================= Text # =================================
If $~ is not specified, the format named STDOUT will be output:
Example
#!/usr/bin/perl write; format STDOUT = ~ The text specified with ~ will not be output ---------------- STDOUT Format ---------------- .
Executing the above example produces the following output:
---------------- STDOUT Format ----------------
The following example demonstrates the use of $^ or $FORMAT_TOP_NAME 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", "", "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 =================================== 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
===================================
.
format EMPLOYEE_TOP =
===================================
Name Age Page @<
$%
===================================
.
select(STDOUT);
$~ = EMPLOYEE;
$^ = EMPLOYEE_TOP;
@n = ("Ali", "", "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 =================================== 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, like this:
write(MYFILE);
The above code uses the print format named MYFILE by default to output to the file MYFILE.
However, this way we 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;
format MYFILE =
=================================
Input to File
=================================
.
close MYFILE;
}
After successful execution, we can view the contents of the tmp file as follows:
$ cat tmp ================================= Input to File =================================
We can use select to change the default file variable. It returns the internal representation of the current default file variable, so we can create subroutines to output as we wish without affecting other parts of the program.
Example
#!/usr/bin/perl
if(open(MYFILE, ">>tmp")){
select(MYFILE);
$~ = "OTHER";
write;
format OTHER =
=================================
Input to File Using Defined Format
=================================
.
close MYFILE;
}
After successful execution, we can view the contents of the tmp file as follows:
$ cat tmp ================================= Input to File ================================= ================================= Input to File Using Defined Format =================================
YouTip