我在Perl中有一个简单的“写入文件”。但是文件永远不会写在if语句中。它写出了if语句。如果始终满足条件,则正常打印有效。任何线索?
outside if, write to file
输出
import re
str='¾ïúÀï{"id":"xxx","timestamp":xxx,"payloadType":"xxx","payload":{"protocol":"xxx","zoneID":xxx,"zoneName":"xxx","eventType":"xxx"}}’ÂCº¾ïúÀï{"id":"xxx","timestamp":xxx,"payloadType":"xxx","payload":{"protocol":"xxx","zoneID":xxx,"zoneName":"xxx","eventType":"xx}}'
str=re.sub('[^\x00-\x7F]','',str)
print(str)
文件内容
'{"id":"xxx","timestamp":xxx,"payloadType":"xxx","payload":{"protocol":"xxx","zoneID":xxx,"zoneName":"xxx","eventType":"xxx"}}C{"id":"xxx","timestamp":xxx,"payloadType":"xxx","payload":{"protocol":"xxx","zoneID":xxx,"zoneName":"xxx","eventType":"xx}}'
答案 0 :(得分:6)
显而易见,condition
是ties文件处理FH到禁用print
的包中的函数。
这是一个原型:
$files = $ARGV[0];
sub condition { tie *FH, 'print_disabler'; 1 }
sub print_disabler::TIEHANDLE { bless {},shift }
sub print_disabler::PRINT { return }
open (FH, '>>', $files) or die $!;
print FH "outside if, write to file";
if (condition){
print FH "inside if, write to file";
print "inside if, normal print";
};
$ perl condition.pl foo
inside if, normal print
$ cat foo
outside if, write to file