Perl formatted output


Release date:2023-10-19 Update date:2023-10-21 Editor:admin View counts:241

Label:

Perl formatted output

Perl is a very powerful text data processing language.

Perl can be used in the format to define a template and then use the write output data according to the specified template.

Perl format definition syntax is 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, such as @, ^, <, >,|.

  • value_one,value_two…… : Data rows, which are used to insert values into the previous format rows, are all perl variables.

  • . :End symbol.

Here is a simple example of formatting:

Example

#!/usr/bin/perl$text="google runoob taobao";formatSTDOUT=first: ^<<<<<#
Left aligned, character length 6$textsecond: ^<<<<<#
Left aligned, character length 6$textthird: ^<<<<# Left aligned,
with a character length of 5,taobao
The last o is truncated$text.write

The output result of executing the above example is:

first: google
second: runoob
third: taoba

Format line (drawing line) syntax

  • Format lines begin with @ or ^, and these lines are not replaced by variables of any kind.

  • The @ field (not to be confused with the array symbol @) is a normal field.

  • The <, >,| length after @, ^ determines the length of the field. If the variable exceeds the defined length, it will be truncated.

  • <, >,| also means left alignment, right alignment, and center alignment, respectively.

  • The ^ field is used for multiline text block filling.

Range format

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

Format

Range meaning

@ <

Left-aligned output

@ >

Right-aligned output

@ |

Align output in

@##.##

Fixed precision number

@ *

Multiline text

The first character of each range is a line filler, and when the @ characteris used, the text is not formatted.

In the above table, except for the multiline range @ *, the width of the field is equal to the specified number of characters including the character@, for example:

@###.##

Represents seven characters wide, four before the decimal point and two after the decimal point.

Examples are as follows:

Example

#!/usr/bin/perlformatEMPLOYEE= ===================================
@<<<<<<<<<<<<<<<<<<<<<<
@<<$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 result of the above example is:

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

Format variable

  • $~ ($FORMAT_NAME) : Format name $^ ($FORMAT_TOP_NAME) : The current header format name is stored in the

  • $% ($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 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

Here is a simple way to use the $~ example of formatting:

Example

#!/usr/bin/perl$~ ="MYFORMAT";# Specify the format used under
the default file variable write;#
output $~ The specified formatformatMYFORMAT=# Define Format
MYFORMAT=================================Text#
Novice Tutorial================================= .write;

The output result of executing the above example is:

=================================
      Text # Novice Tutorial
=================================
=================================
      Text # Novice Tutorial
=================================

If you do not specify $~ in this case, the output is named STDOUT format of:

Example

#!/usr/bin/perlwrite;#
Not specified In the case of $~,
we will search for a format named STDOUT format STDOUT=
~The text specified with the ~ sign will not be output ----------------STDOUT format ---------------- .

The output result of executing the above example is:

----------------
  STDOUT format
----------------

The following example is demonstrated by adding report header information $^ or $FORMAT_TOP_NAME use of variables:

Example

#!/usr/bin/perlformatEMPLOYEE= ===================================
@<<<<<<<<<<<<<<<<<<<<<<
@<<$name,$age@#####.##$salary===================================
.formatEMPLOYEE_TOP=
===================================NameAge===================================
.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 result of the above example is:

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

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

Example

#!/usr/bin/perlformatEMPLOYEE= ===================================
@<<<<<<<<<<<<<<<<<<<<<<
@<<$name,$age@#####.##$salary=================================== .#
Add pagination $%formatEMPLOYEE_TOP=
===================================NameAgePage@< $%
=================================== .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 result of the above example is:

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

Export to another file

Functions by default write output the results to a standard output file STDOUT , we can also make it output the results to any other file the easiest way is to pass the file variable as an argument to the write , such as:

write(MYFILE);

The above code write is output to the file MYFILE in a print format named MYFILE by default.

But it doesn’t work this way. $~ variable to change the print format used. System variable $~ only works on the default file variable, we can change the default file variable, change the $~ , and then call write .

Example

#!/usr/bin/perlif(open(MYFILE,">tmp")){$~ ="MYFORMAT";writeMYFILE;#
Output containing file variables will print the format with the same name
as the variable,that is MYFILE。The value specified in $~is ignored. formatMYFILE=#
Same name as file variable================================= Input into file
================================= .closeMYFILE;}

After the execution is successful, we can check tmp contents of thefile are as follows:

$ cat tmp
=================================
      Input into file
=================================

We can use it select when you change the default file variable, it returns the internal representation of the current default file variable, sothat we can create subroutines and output as we want without affecting the rest of the program.

Example

#!/usr/bin/perlif(open(MYFILE,">>tmp")){select(MYFILE);#
Make the default file variable printout to MYFILE in$~ ="OTHER";write;#
The default file variable, printed to the file specified by select,
must use the format specified by $~
OTHERformatOTHER= =================================
Input into a file using a defined format =================================
.closeMYFILE;}

After the execution is successful, we can check tmp contents of thefile are as follows:

$ cat tmp
=================================
      Input into file
=================================
=================================
  Input into a file using a defined format
=================================

Powered by TorCMS (https://github.com/bukun/TorCMS).