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!
分享到:
相关推荐
官网2016年5月24日更新。由斯坦福大学医学院基于Java语言开发的本体编辑和知识获取软件,它是一个开源软件。Protégé提供了本地概念累,关系,属性和实例的构建,并且屏蔽了具体的本体描述语言,用户只需要在概念...
1,配置adb环境变量Android和path (如:变量名:Android 变量值:E:\Android\sdk\platform-tools;E:\Android\sdk\tools; 变量名:path 变量值: E:\Android\sdk\platform-tools;E:\Android\sdk\tools;) 2,在...
junit-platform-launcher-1.7.0.jarjunit-platform-launcher-1.7.0.jar
赠送jar包:junit-platform-launcher-1.8.0-M1.jar; 赠送原API文档:junit-platform-launcher-1.8.0-M1-javadoc.jar; 赠送源代码:junit-platform-launcher-1.8.0-M1-sources.jar; 赠送Maven依赖信息文件:junit-...
platform-tools_r31.0.3-windows.zip platform-tools是很多刷机的朋友都会用到的工具包,Android SDK其中的platform-tools包可以对安卓设备进行调试,包括解锁、刷机等操作都可以使用这个工具包来进行,其中包含开发...
junit-platform-launcher-1.6.2.jarjunit-platform-launcher-1.6.2.jarjunit-platform-launcher-1.6.2.jar
赠送jar包:junit-platform-commons-1.8.2.jar; 赠送原API文档:junit-platform-commons-1.8.2-javadoc.jar; 赠送源代码:junit-platform-commons-1.8.2-sources.jar; 赠送Maven依赖信息文件:junit-platform-...
Android SDK Platform-Tools is a component for the Android SDK. It includes tools that interface with the Android platform, such as adb, fastboot, and systrace. These tools are required for Android app...
赠送jar包:junit-platform-launcher-1.8.0-M1.jar; 赠送原API文档:junit-platform-launcher-1.8.0-M1-javadoc.jar; 赠送源代码:junit-platform-launcher-1.8.0-M1-sources.jar; 赠送Maven依赖信息文件:junit-...
赠送jar包:junit-platform-commons-1.8.2.jar; 赠送原API文档:junit-platform-commons-1.8.2-javadoc.jar; 赠送源代码:junit-platform-commons-1.8.2-sources.jar; 赠送Maven依赖信息文件:junit-platform-...
赠送jar包:junit-platform-launcher-1.6.1.jar; 赠送原API文档:junit-platform-launcher-1.6.1-javadoc.jar; 赠送源代码:junit-platform-launcher-1.6.1-sources.jar; 赠送Maven依赖信息文件:junit-platform-...
赠送jar包:junit-platform-engine-1.8.2.jar; 赠送原API文档:junit-platform-engine-1.8.2-javadoc.jar; 赠送源代码:junit-platform-engine-1.8.2-sources.jar; 赠送Maven依赖信息文件:junit-platform-engine...
junit-platform-engine-1.6.3.jarjunit-platform-engine-1.6.3.jar
junit-platform-launcher-1.3.1.jar
赠送jar包:junit-platform-engine-1.5.2.jar; 赠送原API文档:junit-platform-engine-1.5.2-javadoc.jar; 赠送源代码:junit-platform-engine-1.5.2-sources.jar; 赠送Maven依赖信息文件:junit-platform-engine...
Android platform-tools 28.0.1 for mac, platform-tools_r28.0.1-darwin.zip
赠送jar包:jna-platform-5.10.0.jar; 赠送原API文档:jna-platform-5.10.0-javadoc.jar; 赠送源代码:jna-platform-5.10.0-sources.jar; 赠送Maven依赖信息文件:jna-platform-5.10.0.pom; 包含翻译后的API文档...
jna-platform-5.5.0.jar
platform-tools_r10-windows 最新的版本 android sdk平台工具
platform-tools-r22-window.rar文件是SDK中的platform tools的22版本升级文件夹,解压后替换SDK的platform-tools目录