使用格式处理器按需打印一行,其中没有变量
Print a line on demand without variable in it using format processor
我正在编写一个不使用 perl 格式处理器的协议。
所以我有这样的格式
format err_spooler_line =
@#### | Error: ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$error_spooler_line, $error_spooler_text_short
~~ | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$error_spooler_text_short
# --> This line should only displayed, when $error_spooler_text_long is set.
| Details:
~~ | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$error_spooler_text_long
.
作为解决方法 我使用:
format err_spooler_line =
@#### | Error: ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$error_spooler_line, $error_spooler_text_short
~~ | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$error_spooler_text_short
# --> This line should only displayed, when $error_spooler_text_long is set.
# So it is working, but it writes some text of the description in the line
~ | Details: ^<
$error_spooler_text_long
~~ | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$error_spooler_text_long
.
如果我用 \n
开始我的描述测试,它会起作用:
$error_spooler_text_long = "\n" . $error_spooler_text_long
。所以在“详细信息”之后有一个换行符,下一行从下一个图片行开始。
但是我怎样才能自动执行此操作?这样就不需要在我的字符串前加上 \n
.
一个完整的例子:
#!usr/bin/perl
use strict;
# Variables used in the format
my $error_spooler_line;
my $error_spooler_text_short;
my $error_spooler_text_long;
format err_spooler_top =
Protocol: - DATA-Error Page: @###
$%
--------------------------------------------------------------
.
format err_spooler_line =
@#### | Error: ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$error_spooler_line, $error_spooler_text_short
~~ | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$error_spooler_text_short
| Details:
~~ | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$error_spooler_text_long
.
# The whole protocol is written into a string.
my $error_file_string = "";
open my $hnd_spooler, ">", $error_file_string;
select((select($hnd_spooler),
$~ = "err_spooler_line",
$^ = "err_spooler_top"
)[0]);
while(<DATA>) {
chomp;
$error_spooler_line = $.;
($error_spooler_text_short, $error_spooler_text_long) = split (/;/);
write $hnd_spooler;
}
close ($hnd_spooler);
## Now, all the protocol is in $error_file_string !
print $error_file_string;
__DATA__
ERR_ID_0 OK;
ERR_ID_278 UPDATE Failed;Update failed cause of DB connection error\nDB error number: 22.
ERR_ID_0 OK;
ERR_ID_33 Invalid data format;Only numbers allowed.
那么输出是:
Protocol: - DATA-Error Page: 1
--------------------------------------------------------------
1 | Error: ERR_ID_0 OK
| Details:
2 | Error: ERR_ID_278 UPDATE Failed
| Details:
| Update failed cause of DB connection error\nDB error
| number: 22.
3 | Error: ERR_ID_0 OK
| Details:
4 | Error: ERR_ID_33 Invalid data format
| Details:
| Only numbers allowed.
但我想要“详细信息:”行只有它有详细信息:
Protocol: - DATA-Error Page: 1
--------------------------------------------------------------
1 | Error: ERR_ID_0 OK
2 | Error: ERR_ID_278 UPDATE Failed
| Details:
| Update failed cause of DB connection error
| DB error number: 22.
3 | Error: ERR_ID_0 OK
4 | Error: ERR_ID_33 Invalid data format
| Details:
| Only numbers allowed.
根据perlform:
Using caret fields can produce lines where all fields are blank. You
can suppress such lines by putting a "~" (tilde) character anywhere in
the line. The tilde will be translated to a space upon output.
以下似乎有效:
use strict;
use warnings;
my $error_spooler_line ;
my $error_spooler_text_short;
my $error_spooler_text_long;
format err_spooler =
@#### | Error: ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$error_spooler_line, $error_spooler_text_short
~~ | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$error_spooler_text_short
~ | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
@{[(length $error_spooler_text_long) ? "Details:" : ""]}
~~ | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$error_spooler_text_long
.
select (STDOUT);
$~ = "err_spooler";
while(<DATA>) {
chomp;
$error_spooler_line = $.;
($error_spooler_text_short, $error_spooler_text_long) = split /;/;
write;
}
__DATA__
ERR_ID_0 OK;
ERR_ID_278 UPDATE Failed;Update failed cause of DB connection error, DB error number: 22.
ERR_ID_0 OK;
ERR_ID_33 Invalid data format;Only numbers allowed.
输出:
1 | Error: ERR_ID_0 OK
2 | Error: ERR_ID_278 UPDATE Failed
| Details:
| Update failed cause of DB connection error, DB error number: 22.
3 | Error: ERR_ID_0 OK
4 | Error: ERR_ID_33 Invalid data format
| Details:
| Only numbers allowed.
我正在编写一个不使用 perl 格式处理器的协议。
所以我有这样的格式
format err_spooler_line =
@#### | Error: ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$error_spooler_line, $error_spooler_text_short
~~ | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$error_spooler_text_short
# --> This line should only displayed, when $error_spooler_text_long is set.
| Details:
~~ | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$error_spooler_text_long
.
作为解决方法 我使用:
format err_spooler_line =
@#### | Error: ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$error_spooler_line, $error_spooler_text_short
~~ | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$error_spooler_text_short
# --> This line should only displayed, when $error_spooler_text_long is set.
# So it is working, but it writes some text of the description in the line
~ | Details: ^<
$error_spooler_text_long
~~ | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$error_spooler_text_long
.
如果我用 \n
开始我的描述测试,它会起作用:
$error_spooler_text_long = "\n" . $error_spooler_text_long
。所以在“详细信息”之后有一个换行符,下一行从下一个图片行开始。
但是我怎样才能自动执行此操作?这样就不需要在我的字符串前加上 \n
.
一个完整的例子:
#!usr/bin/perl
use strict;
# Variables used in the format
my $error_spooler_line;
my $error_spooler_text_short;
my $error_spooler_text_long;
format err_spooler_top =
Protocol: - DATA-Error Page: @###
$%
--------------------------------------------------------------
.
format err_spooler_line =
@#### | Error: ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$error_spooler_line, $error_spooler_text_short
~~ | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$error_spooler_text_short
| Details:
~~ | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$error_spooler_text_long
.
# The whole protocol is written into a string.
my $error_file_string = "";
open my $hnd_spooler, ">", $error_file_string;
select((select($hnd_spooler),
$~ = "err_spooler_line",
$^ = "err_spooler_top"
)[0]);
while(<DATA>) {
chomp;
$error_spooler_line = $.;
($error_spooler_text_short, $error_spooler_text_long) = split (/;/);
write $hnd_spooler;
}
close ($hnd_spooler);
## Now, all the protocol is in $error_file_string !
print $error_file_string;
__DATA__
ERR_ID_0 OK;
ERR_ID_278 UPDATE Failed;Update failed cause of DB connection error\nDB error number: 22.
ERR_ID_0 OK;
ERR_ID_33 Invalid data format;Only numbers allowed.
那么输出是:
Protocol: - DATA-Error Page: 1
--------------------------------------------------------------
1 | Error: ERR_ID_0 OK
| Details:
2 | Error: ERR_ID_278 UPDATE Failed
| Details:
| Update failed cause of DB connection error\nDB error
| number: 22.
3 | Error: ERR_ID_0 OK
| Details:
4 | Error: ERR_ID_33 Invalid data format
| Details:
| Only numbers allowed.
但我想要“详细信息:”行只有它有详细信息:
Protocol: - DATA-Error Page: 1
--------------------------------------------------------------
1 | Error: ERR_ID_0 OK
2 | Error: ERR_ID_278 UPDATE Failed
| Details:
| Update failed cause of DB connection error
| DB error number: 22.
3 | Error: ERR_ID_0 OK
4 | Error: ERR_ID_33 Invalid data format
| Details:
| Only numbers allowed.
根据perlform:
Using caret fields can produce lines where all fields are blank. You can suppress such lines by putting a "~" (tilde) character anywhere in the line. The tilde will be translated to a space upon output.
以下似乎有效:
use strict;
use warnings;
my $error_spooler_line ;
my $error_spooler_text_short;
my $error_spooler_text_long;
format err_spooler =
@#### | Error: ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$error_spooler_line, $error_spooler_text_short
~~ | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$error_spooler_text_short
~ | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
@{[(length $error_spooler_text_long) ? "Details:" : ""]}
~~ | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$error_spooler_text_long
.
select (STDOUT);
$~ = "err_spooler";
while(<DATA>) {
chomp;
$error_spooler_line = $.;
($error_spooler_text_short, $error_spooler_text_long) = split /;/;
write;
}
__DATA__
ERR_ID_0 OK;
ERR_ID_278 UPDATE Failed;Update failed cause of DB connection error, DB error number: 22.
ERR_ID_0 OK;
ERR_ID_33 Invalid data format;Only numbers allowed.
输出:
1 | Error: ERR_ID_0 OK
2 | Error: ERR_ID_278 UPDATE Failed
| Details:
| Update failed cause of DB connection error, DB error number: 22.
3 | Error: ERR_ID_0 OK
4 | Error: ERR_ID_33 Invalid data format
| Details:
| Only numbers allowed.