[Perl] format output format

grammar


format FORMATNAME =
fieldline
value1, value2, value3
fieldline
value1, value2, value3
.
 
=pod
FORMATNAME :格式化模板名称
fieldline  :一个格式行,用来定义一个输出行的格式,以@或^开头
value1..    : 数据行,向前面一个格式行插入变量value
.          :结束符号


Format line syntax

  • Or beginning with @ ^
  • @, After ^ <,>, | determines the length of the length field, if the variable exceeds the defined length will be truncated
  • <,>, | Also represent left-aligned, right-aligned, centered
  • ^ For multiple lines of text fill

Range format

@ ### ### represents a fixed-precision numbers, a total of eight zifu characters wide, four before the decimal point, the back 3


Variable Format

$ ~ ($ FORMAT_NAME): the name of the current format

$ ^ ($ FORMAT_TOP_NAME): the current header format

$ = ($ FORMAT_LINES_PER_PAGE): the number of lines per page in

$ | ($ FORMAT_AUTOFLUSH): whether to automatically flush the output buffer memory

$% ($ FORMAT_PAGE_NUMBER): Set Page


The default format

    The default format is STDOUT, change the output file handle with select, remember and write with use.


example

#!/usr/bin/perl -w

   format STUDENT =
   ==========================
   @<<<<<<<<<<<<< @<<<<<<<<<<
   $name, $score
   ==========================
   .
   
   format STUDENT_TOP =
   ==========================
   Name           Score
   ==========================
   .
 
   @stuName = ('hunk', 'jack', 'lucy');
   @stuScore = (80, 70 , 60);
  
   if(open(STUDENT, ">tmp")){
      #select(HANDLE);
      $~ = STUDENT;    #指定格式名字
      $^ = STUDENT_TOP;#指定表頭格式
      $i = 0;
      foreach (@stuName){
          $name = $_;
          $score = $stuScore[$i++];
          write STUDENT;
      }
      close STUDENT;
  }

  result:

==========================
Name           Score
==========================
==========================
hunk           80
==========================
==========================
jack           70
==========================
==========================
lucy           60
==========================


 

Published 89 original articles · won praise 17 · views 40000 +

Guess you like

Origin blog.csdn.net/lbt_dvshare/article/details/90545727