perl格式化报表输出
1.字段标志符号文章源自运维生存时间-https://www.ttlsa.com/perl/perl-format-report/
@---表示字段的起始文章源自运维生存时间-https://www.ttlsa.com/perl/perl-format-report/
@*---多行字段文章源自运维生存时间-https://www.ttlsa.com/perl/perl-format-report/
^---用于填充字段文章源自运维生存时间-https://www.ttlsa.com/perl/perl-format-report/
2.字段显示符号文章源自运维生存时间-https://www.ttlsa.com/perl/perl-format-report/
<---左对齐文章源自运维生存时间-https://www.ttlsa.com/perl/perl-format-report/
>---右对齐文章源自运维生存时间-https://www.ttlsa.com/perl/perl-format-report/
|---居中文章源自运维生存时间-https://www.ttlsa.com/perl/perl-format-report/
#---数字文章源自运维生存时间-https://www.ttlsa.com/perl/perl-format-report/
.---小数点位置文章源自运维生存时间-https://www.ttlsa.com/perl/perl-format-report/
默认的文件句柄是STDOUT.文章源自运维生存时间-https://www.ttlsa.com/perl/perl-format-report/
3.创建模版文章源自运维生存时间-https://www.ttlsa.com/perl/perl-format-report/
格式模版结构如下:文章源自运维生存时间-https://www.ttlsa.com/perl/perl-format-report/
format FILEHANDLE=文章源自运维生存时间-https://www.ttlsa.com/perl/perl-format-report/
picture line(定义形象行)文章源自运维生存时间-https://www.ttlsa.com/perl/perl-format-report/
value line(格式化输出文本)文章源自运维生存时间-https://www.ttlsa.com/perl/perl-format-report/
.(结束)文章源自运维生存时间-https://www.ttlsa.com/perl/perl-format-report/
write;(调用格式模版)文章源自运维生存时间-https://www.ttlsa.com/perl/perl-format-report/
4.定义页眉格式文章源自运维生存时间-https://www.ttlsa.com/perl/perl-format-report/
会在每一页的顶部打印出来,页面的默认长度为60行。改变$=的值就可以修改页面长度。文章源自运维生存时间-https://www.ttlsa.com/perl/perl-format-report/
例如:
[codesyntax lang="perl"]
format STDOUT_TOP=
数据迁移校验明细-@||
$%
====================
+---------------------+-----------+-----------------------+---------+--------+
| From_table_name | From_rows | To_table_name | To_rows | Result |
+---------------------+-----------+-----------------------+---------+--------+
.
[/codesyntax]
注释:$%perl特殊变量,含有当前的页码。
5.定义文档格式
例如:
[codesyntax lang="perl"]
format STDOUT=
|@<<<<<<<<<<<<<<<<<<<<| @<<<<<<<<<| @<<<<<<<<<<<<<<<<<<<<<| @<<<<<<<| @<<<<<<|
$f_table, $f_num,$t_table, $t_num, $result
+---------------------+-----------+-----------------------+---------+--------+
.
[/codesyntax]
注释:@<定义字符长度和对齐格式,@也代表一个字符。
[codesyntax lang="perl"]
#!/usr/bin/perl
###################################
### author: www.ttlsa.com ###
### QQ群:39514058 ###
### E-mail:service@ttlsa.com ###
###################################
use DBI;
&check_ttlsa_user;
sub init{
my $driver="DBI:mysql";
my ($dbname,$host,$user,$password)=@_;
return $dbh=DBI->connect("$driver:$dbname:$host;user=$user;password=$password",{PrintError => 0, RaiseError => 1}) or die "Can't connect";
}
sub check_ttlsa_user{
for (0..99) {
local $f_table="ttlsa_user_$_";
my $fsql="select sql_no_cache count(uid) from ttlsa_user_$_";
&init('www_ttlsa_com','localhost','root','www.ttlsa.com');
local $f_num=$dbh->selectrow_array($fsql);
$dbh->disconnect();
my $sufix=sprintf "%02d",$_;
local $t_table="ttlsa_user_$sufix";
my $tsql="select sql_no_cache count(uid) from ttlsa_user_$sufix";
&init('www_ttlsa_com','localhost','root','www.ttlsa.com');
local $t_num=$dbh->selectrow_array($tsql);
local $result=(($f_num == $t_num) ? 'OK' : 'ERROR');
write ;
}
}
$from="www.ttlsa.com";
$~=SUMMARY; //切换到另一个格式
write;
format STDOUT_TOP=
- @|| -
$%
=========================
+---------------------+-----------+-----------------------+---------+--------+
| From_table_name | From_rows | To_table_name | To_rows | Result |
+---------------------+-----------+-----------------------+---------+--------+
.
format STDOUT=
|@<<<<<<<<<<<<<<<<<<<<| @<<<<<<<<<| @<<<<<<<<<<<<<<<<<<<<<| @<<<<<<<| @<<<<<<|
$f_table, $f_num,$t_table, $t_num, $result
+---------------------+-----------+-----------------------+---------+--------+
.
format SUMMARY=
| FROM: @<<<<<<<<<<<<<|
$from,
+============================================================================+
.
[/codesyntax]
评论