Platform-independent is Nothing

20 篇文章 0 订阅

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 '\r\n' 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 "@strings\n";
Here is the output running in Linux:

user@desktop:~/home$ ls
file.txt  toy.pl
user@desktop:~/home$ file file.txt 
file.txt: ASCII text, with CRLF line terminators
user@desktop:~/home$ perl toy.pl file.txt 
 betterg
user@desktop:~/home$




if run toy.pl in windows, it get expected result:
z:\Explorer\assets>perl toy.pl
freedom is nothing but a chance to be better
So, Perl is doing the right thing here, the reason is that I shouldn't process Windows' text files directly in Linux, we should have converted it to Unix format or specified $/ to \r\n.

This is a trivial issue, but revealing an idea that real platform independence is nothing. Nothing can run correctly without a change across different platforms. C, C++, so called platform independent, but you have to pay extra attentions to word size, byte order and architecture gaps between different platforms. Java so called "compile once run everywhere", byte code is indeed platform independent, but byte code must run upon Java Virtual Machine, which is absolutely platform dependent, as a result, Java is platform dependent, to some extent at least!


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值