Platform-independent is Nothing
Last Friday I had to process some text files,adding some tags before some patterns in a text file. Perl is born for such a task. I first copied all the patterns into a file in Windows XP,then I switched to Linux to use Perl to finishing the task. But it never worked in the way I expected. The script was simple just reading target file line by line,then testing whether matching the patterns which read from files and chomped,if matching adding the tag. But it never worked,the line never matched the pattern,even they did match. Suddenly,I realized the problem,the file containing the patterns was created in Windows XP,but I read and chomped it in Linux,So newline terminator 'rn' was not chomped completely. As a result,lines never matched patterns except for last pattern,which was last line of that file and did not contain newline. So when ran it in Windows XP,I got expected results. Here is the simple scene,the file is: freedom
is
nothing
but
a
chance
to
be
better
Here is the Perl script:
#!/usr/bin/perl -w
my $src = "file.txt";
open SOURCE,$src or die "failed to open $src,$!";
chomp(@strings = <SOURCE>);
print "@stringsn";
Here is the output running in Linux:
user@desktop:~/home$ ls file.txt toy.pluser@desktop:~/home$ file file.txt file.txt: ASCII text,with CRLF line terminatorsuser@desktop:~/home$ perl toy.pl file.txt betterg (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |