Perl Formatted Output
Perl is a very powerful language for processing text data.
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 explanations:
- FormatName: The name of the format.
- fieldline: A format line used to define the format of an output line, similar to characters like @, ^, <, >, |.
- valueone, valuetwo...: Data lines used to insert values into the preceding format line, all of which are Perl variables.
- .: End symbol.
Here is a simple example of formatting:
Example
#!/usr/bin/perl
$text = "google tutorialpro 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' in 'taobao' is truncated
$text
.
write
Executing the above example outputs:
first: google
second: tutorialpro
third: taoba
Format Line (Picture Line) Syntax
- Format lines start with @ or ^ and do not perform any variable substitution.
- The @ field (not to be confused with the array symbol @) is a regular field.
- The length of the field is determined by the characters <, >, | following @, ^. If the variable exceeds the defined length, it will be truncated.
- <, >, | also respectively indicate left-aligned, right-aligned, and center-aligned.
- The ^ field is used for multi-line text block filling.
Field Format
The format of the fields is as shown in the table below:
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 performed.
In the table above, except for the multi-line field @*, the field width is equal to the number of characters including @, for example:
@###.##
Indicates a width of seven characters, four before the decimal point and two after.
Here is an example:
Example
#!/usr/bin/perl
format EMPLOYEE =
===================================
@<<<<<<<<<<<<<<<<<<<<<< @<<
$name, $age
@#####.##
$salary
===================================
.
select(STDOUT);
$~ = EMPLOYEE;
@n = ("Ali", "tutorialpro", "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
===================================
===================================
tutorialpro 30
2500.00
===================================
===================================
Jaffer 40
4000.00
===================================
Format Variables
- $~ ($FORMAT_NAME): The name of the format.
- $^ ($FORMAT_TOP_NAME): The name of the current header format stored in.
- $% ($FORMAT_PAGE_NUMBER): The current page number being output.
- $= ($FORMAT_LINES_PER_PAGE): The number of lines per page.
- $| ($FORMAT_AUTOFLUSH): Whether to automatically flush the output buffer stored.
- $^L ($FORMAT_FORMFEED): The string to be output before the header on each page (except the first page). Here is a simple example using the $~ formatting:
Example
#!/usr/bin/perl
$~ = "MYFORMAT"; # Specifies the format used under the default file variable
write; # Outputs the format specified by $~
format MYFORMAT = # Defines the format MYFORMAT
=================================
Text # tutorialpro.org
=================================
.
write;
Executing the above example outputs:
=================================
Text # tutorialpro.org
=================================
=================================
Text # tutorialpro.org
=================================
If $~ is not specified, it will output the format named STDOUT:
Example
#!/usr/bin/perl
write; # If $~ is not specified, it will look for the format named STDOUT
format STDOUT =
~Text specified with ~ will not be output
----------------
STDOUT format
----------------
.
Executing the above example outputs:
----------------
STDOUT format
----------------
In the following example, we demonstrate the use of $^ or $FORMAT_TOP_NAME by adding header information to the report:
Example
#!/usr/bin/perl
format EMPLOYEE =
===================================
@<<<<<<<<<<<<<<<<<<<<<< @<<
$name, $age
@#####.##
$salary
===================================
.
format EMPLOYEE_TOP =
===================================
Name Age
===================================
.
select(STDOUT);
$~ = EMPLOYEE;
$^ = EMPLOYEE_TOP;
@n = ("Ali", "tutorialpro", "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 outputs:
===================================
Name Age
===================================
===================================
Ali 20
2000.00
===================================
===================================
tutorialpro 30
2500.00
===================================
===================================
Jaffer 40
4000.00
===================================
We can also set pagination for the report using $% or $FORMAT_PAGE_NUMBER:
Example
#!/usr/bin/perl
format EMPLOYEE =
===================================
@<<<<<<<<<<<<<<<<<<<<<< @<<
$name, $age
@#####.##
$salary
===================================
.
# Adding pagination $%
format EMPLOYEE_TOP =
===================================
Name Age Page @<
This is an example of outputting results 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, like this:
write(MYFILE);
The above code uses the default format named MYFILE to output to the file MYFILE.
However, this prevents us from changing the print format using the $~ variable. The $~ variable only affects 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 with file variable, prints the format with the same name as the variable, i.e., MYFILE. The value specified in $~ is ignored.
format MYFILE = # Same name as the file variable
=================================
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, allowing 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); # Makes the default file variable output to MYFILE
$~ = "OTHER";
write; # Default file variable, outputs to the file specified by select, using the format specified by $~: OTHER
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
=================================