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 namefieldline: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 allperlvariables..:End symbol.
Here is a simple example of formatting: The output result of executing the above example is: 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. 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: The output result of the above example is: Here is a simple way to use the The output result of executing the above example is: If you do not specify The output result of executing the above example is: The following example is demonstrated by adding report header information The output result of the above example is: We can also use it The output result of the above example is: Functions by default 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. After the execution is successful, we can check We can use it After the execution is successful, we can check 5.34.1. 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
first: google second: runoob third: taoba
Format line (drawing line) syntax #
Range format #
@###.##
5.34.2. 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;} =================================== 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 $~ example of formatting: 5.34.3. 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;
================================= Text # Novice Tutorial ================================= ================================= Text # Novice Tutorial =================================
$~ in this case, the output is named STDOUT format of: 5.34.4. 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 ---------------- .
---------------- STDOUT format ----------------
$^ or $FORMAT_TOP_NAME use of variables: 5.34.5. 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;} =================================== Name Age =================================== =================================== Ali 20 2000.00 =================================== =================================== Runoob 30 2500.00 =================================== =================================== Jaffer 40 4000.00 ===================================
$% or $FORMAT_PAGE_NUMBER set up paging for the report: 5.34.6. 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;} =================================== Name Age Page 1 =================================== =================================== Ali 20 2000.00 =================================== =================================== Runoob 30 2500.00 =================================== =================================== Jaffer 40 4000.00 ===================================
Export to another file #
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);
$~ 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;} tmp contents of thefile are as follows:$ cat tmp ================================= Input into file =================================
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. 5.34.7. 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;} tmp contents of thefile are as follows:$ cat tmp ================================= Input into file ================================= ================================= Input into a file using a defined format =================================