如果我$myString用值声明了一个变量'3 '(注意空格).是否有任何函数可以删除返回值的空白区域.有点像SomeFun($myString) 回归'3'(没有白色空间).
#!C:\Perl\bin\perl.exe
use strict;
use warnings;
use Data::Dumper;
my $fh = \*DATA;
print Dumper parse_constant_spec( $fh );
# Parse a constant spec file.
# Pass in a handle to process.
# As long as it acts like a file handle, it will work.
sub parse_constant_spec {
my $fh = shift;
my %spec;
# Until file is done:
# Read in a whole block
while( my $block = read_block($fh) ) {
# …Run Code Online (Sandbox Code Playgroud) 您可以在下面更正我的代码吗?
#!/usr/local/bin/perl
open (MYFILE, '>>data.xml');
print MYFILE "<?xml version="1.0" encoding="UTF-8"?>\n";
close (MYFILE);
Run Code Online (Sandbox Code Playgroud)
更新.
#!/usr/local/bin/perl
open (MYFILE, '>>data.xml');
print MYFILE '<?xml version="1.0" encoding="UTF-8"?\>'."\n";
print MYFILE '<?xml version="1.0" encoding="UTF-16"?\>'."\n";
close (MYFILE);
Run Code Online (Sandbox Code Playgroud)
输出:现在运作良好.
<?xml version="1.0" encoding="UTF-8"?\>
<?xml version="1.0" encoding="UTF-16"?\>
Run Code Online (Sandbox Code Playgroud)
但.
#!/usr/local/bin/perl
open (MYFILE, '>>data.xml');
print MYFILE '<?xml version="1.0" encoding="UTF-8"?\>'.'\n';
print MYFILE '<?xml version="1.0" encoding="UTF-16"?\>'.'\n';
close (MYFILE);
Run Code Online (Sandbox Code Playgroud)
输出:#n错误格式\n
<?xml version="1.0" encoding="UTF-8"?\>\n<?xml version="1.0" encoding="UTF-16"?\>\n
Run Code Online (Sandbox Code Playgroud) 美好的一天.
我的文本文件内容如下.tmp.txt(一个非常大的文件)
constant fixup private AlarmFileName = <A "C:\\TMP\\ALARM.LOG"> /* A Format */
constant fixup ConfigAlarms = <U1 0> /* U1 Format */
constant fixup ConfigEvents = <U2 0> /* U2 Format */
Run Code Online (Sandbox Code Playgroud)
我的解析代码如下.这里的代码无法处理C:\\TMP\\ALARM.LOG(带引号的字符串).我不知道如何替换代码"s +([a-zA-Z0-9])+>"来处理[a-zA-Z0-9](上面的0)字符串和quated字符串(" C:\ TMP\ALARM.LOG"上面).
$source_file = "tmp.txt";
$dest_xml_file = "my.xml";
#Check existance of root directory
open(SOURCE_FILE, "$source_file") || die "Fail to open file $source_file";
open(DEST_XML_FILE, ">$dest_xml_file") || die "Coult not open output file $dest_xml_file";
$x = 0;
print DEST_XML_FILE "<!-- from tmp.txt-->\n";
while (<SOURCE_FILE>)
{ …Run Code Online (Sandbox Code Playgroud)