如何重新初始化 perls 格式处理器
How to reinitialize perls format processor
更新:从“DATA”读取更改为从 sub 获取数据。
我必须为打印机编写协议。打印机需要一个字符串才能打印,所以我用的是perls格式处理器
这是第一次,效果很好。但是如果我试着写协议两次,
打印的数据格式不正确。
我试过的代码示例:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use utf8;
my $a;
my $b;
my $c;
my $protocol;
my $h_protocol;
format F_TOP =
---------------------------
1 2 3 P: @
$%
---------------------------
.
format F_ENTRY =
@<<< @<<< @<<<
$a, $b, $c
.
print_protocol("-1-"); # First run
print_protocol("-2-"); # Second run - should gibe the same output
sub print_protocol {
my $run = shift;
print "$run\n";
# Open protocol string as filehandle
open $h_protocol, ">", $protocol;
# Set format to protocol filehandle
select((select($h_protocol), # Filehandle
$~ = "F_ENTRY",
$^ = "F_TOP",
$= = 5 # Lines per page
)[0]);
# Write data in protocol "file"
my @data = split /\n/, get_data();
for (@data) {
($a, $b, $c) = split /;/;
write $h_protocol;
}
# close filehandle
close $h_protocol;
# Print the protocol (to STDOUT to test)
print $protocol;
# Delete protocol data
$protocol = "";
}
sub get_data {
return "abc;def;ghi\njkl;mno;pqr\nabc;def;ghi\njkl;mno;pqr\nqwe;eqw;weq";
}
我得到的输出是:
-1-
---------------------------
1 2 3 P: 1
---------------------------
abc def ghi
jkl mno pqr
♀---------------------------
1 2 3 P: 2
---------------------------
abc def ghi
jkl mno pqr
♀---------------------------
1 2 3 P: 3
---------------------------
qwe eqw weq
-2-
abc def ghi
jkl mno pqr
abc def ghi
jkl mno pqr
qwe eqw weq
那么如何重置协议句柄/协议处理器,以便正确打印数据。
-1-
---------------------------
1 2 3 P: 1
---------------------------
abc def ghi
jkl mno pqr
♀---------------------------
1 2 3 P: 2
---------------------------
abc def ghi
jkl mno pqr
♀---------------------------
1 2 3 P: 3
---------------------------
qwe eqw weq
-2-
---------------------------
1 2 3 P: 1
---------------------------
abc def ghi
jkl mno pqr
♀---------------------------
1 2 3 P: 2
---------------------------
abc def ghi
jkl mno pqr
♀---------------------------
1 2 3 P: 3
---------------------------
qwe eqw weq
当然,实际上打印机不是“STDOUT”。
我可以复制你在问题中提到的问题。
只需在 print_protocol()
sub 中声明 $h_protocol
FH。并将其从第 12 行中删除。
因此您在 sub 中的代码将如下所示。
sub print_protocol {
my $run = shift;
print "$run\n";
# Open protocol string as filehandle
open my $h_protocol, ">", $protocol;
# Set format to protocol filehandle
...
...
}
现在脚本可以生成预期结果了。
输出:
-1-
---------------------------
1 2 3 P: 1
---------------------------
abc def ghi
jkl mno pqr
---------------------------
1 2 3 P: 2
---------------------------
abc def ghi
jkl mno pqr
---------------------------
1 2 3 P: 3
---------------------------
qwe eqw weq
-2-
---------------------------
1 2 3 P: 1
---------------------------
abc def ghi
jkl mno pqr
---------------------------
1 2 3 P: 2
---------------------------
abc def ghi
jkl mno pqr
---------------------------
1 2 3 P: 3
---------------------------
qwe eqw weq
更新:从“DATA”读取更改为从 sub 获取数据。
我必须为打印机编写协议。打印机需要一个字符串才能打印,所以我用的是perls格式处理器
这是第一次,效果很好。但是如果我试着写协议两次, 打印的数据格式不正确。
我试过的代码示例:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use utf8;
my $a;
my $b;
my $c;
my $protocol;
my $h_protocol;
format F_TOP =
---------------------------
1 2 3 P: @
$%
---------------------------
.
format F_ENTRY =
@<<< @<<< @<<<
$a, $b, $c
.
print_protocol("-1-"); # First run
print_protocol("-2-"); # Second run - should gibe the same output
sub print_protocol {
my $run = shift;
print "$run\n";
# Open protocol string as filehandle
open $h_protocol, ">", $protocol;
# Set format to protocol filehandle
select((select($h_protocol), # Filehandle
$~ = "F_ENTRY",
$^ = "F_TOP",
$= = 5 # Lines per page
)[0]);
# Write data in protocol "file"
my @data = split /\n/, get_data();
for (@data) {
($a, $b, $c) = split /;/;
write $h_protocol;
}
# close filehandle
close $h_protocol;
# Print the protocol (to STDOUT to test)
print $protocol;
# Delete protocol data
$protocol = "";
}
sub get_data {
return "abc;def;ghi\njkl;mno;pqr\nabc;def;ghi\njkl;mno;pqr\nqwe;eqw;weq";
}
我得到的输出是:
-1-
---------------------------
1 2 3 P: 1
---------------------------
abc def ghi
jkl mno pqr
♀---------------------------
1 2 3 P: 2
---------------------------
abc def ghi
jkl mno pqr
♀---------------------------
1 2 3 P: 3
---------------------------
qwe eqw weq
-2-
abc def ghi
jkl mno pqr
abc def ghi
jkl mno pqr
qwe eqw weq
那么如何重置协议句柄/协议处理器,以便正确打印数据。
-1-
---------------------------
1 2 3 P: 1
---------------------------
abc def ghi
jkl mno pqr
♀---------------------------
1 2 3 P: 2
---------------------------
abc def ghi
jkl mno pqr
♀---------------------------
1 2 3 P: 3
---------------------------
qwe eqw weq
-2-
---------------------------
1 2 3 P: 1
---------------------------
abc def ghi
jkl mno pqr
♀---------------------------
1 2 3 P: 2
---------------------------
abc def ghi
jkl mno pqr
♀---------------------------
1 2 3 P: 3
---------------------------
qwe eqw weq
当然,实际上打印机不是“STDOUT”。
我可以复制你在问题中提到的问题。
只需在 print_protocol()
sub 中声明 $h_protocol
FH。并将其从第 12 行中删除。
因此您在 sub 中的代码将如下所示。
sub print_protocol {
my $run = shift;
print "$run\n";
# Open protocol string as filehandle
open my $h_protocol, ">", $protocol;
# Set format to protocol filehandle
...
...
}
现在脚本可以生成预期结果了。
输出:
-1-
---------------------------
1 2 3 P: 1
---------------------------
abc def ghi
jkl mno pqr
---------------------------
1 2 3 P: 2
---------------------------
abc def ghi
jkl mno pqr
---------------------------
1 2 3 P: 3
---------------------------
qwe eqw weq
-2-
---------------------------
1 2 3 P: 1
---------------------------
abc def ghi
jkl mno pqr
---------------------------
1 2 3 P: 2
---------------------------
abc def ghi
jkl mno pqr
---------------------------
1 2 3 P: 3
---------------------------
qwe eqw weq