用perl脚本解决ant…非法字符:\65279…的问题
用ant管理项目,编译时发现有些utf8编码的java文件无法编译,
报错是…非法字符:\65279…。
在网上查N多与ant有关的文章,但是大多数的解决方案都不是很理想。因为文件比较多,而且我的
机器上也没有装editplus。
索性自己用perl写了一个脚本
如下:
#remove the utf-8 BOM for Ant
sub remove_bom {
#open( my $in, "+<", "src/com/umpay/wap20/mobileProxy/MobileProxy.java" );
foreach my $filename (@_) {
print $filename,"\n";
open( my $in, "+<", $filename );
@lines = <$in>;
$line1 = $lines[0];
$t_chk1 = substr( $line1, 0, 1 ); #0xef -> 239
$t_chk2 = substr( $line1, 1, 1 ); #0xbb -> 187
$t_chk3 = substr( $line1, 2, 1 ); #0xbf ->191
if ( ord($t_chk1) == 239 && ord($t_chk2) == 187 && ord($t_chk3) == 191 )
{
$line1 = substr( $line1, 3 );
seek( $in, 0, 0 );
print $in $line1;
}
close $in;
}
}
&remove_bom("src/com/umpay/wap20/mobileProxy/MobileProxy.java"
,"src/com/umpay/wap20/security/GeneralKeyPairs.java"
,"src/com/umpay/wap20/security/RSAUtil.java"
,"src/com/umpay/wap/pages/WML.java");
其中remove_bom方法就是处理有问题的文件的方法。
把你的项目中出现问题的文件名称一个一个的放到remove_bom的参数列表中就可以了。ant,BOM,ant,BOM
最后运行这个perl文件。之后再运行ant就可以了。
这次春节回家,什么资料也没带,刚好机器里也没什么东东,只有之前装的perl,索性翻了翻perl的文档,发现和ruby最相似的语言原来是perl,应该是ruby抄袭的perl。
Perl的语法要求:每一句后最后都要有分号,最后一句可以省略分号。
Perl 的数据类型包括:Scalars,arrays,hashes。
Scalar:就是变量
Scalar 值可以是字符串,整形或者浮点型。Perl会自动转型,换句话说,perl是若类型语言。但是如果你要定义一个变量,需要使用my这个关键字。使用起来也极其简单。
Scalar values can be used in various ways:
my $animal = "camel";
my $answer = 42;
print $animal;
print "The animal is $animal\n";
print "The square of $answer is ", $answer * $answer, "\n";
一般脚本语言都会预定义一些变量。如果你用过ruby或者python,那你一定深有体会。
在perl中也有一些预定义的变量,比如$_就是预定义值。
试一下这两句,你就知道了。
说实话,这种预定义的特性,对于System Administrator来说是一个好用的属性,但是对于Developer来说,真是很痛苦的一个特性。
Array:就是数组
my @animals = ("camel", "llama", "owl");
my @numbers = (23, 42, 69);
my @mixed = ("camel", 42, 1.23);
print $animals[0]; # prints "camel"
print $animals[1]; # prints "llama"
$#array 为最后一个数组元素的下标。例如:
print $mixed[$#mixed]; # last element, prints 1.23
之前就听说perl的语法及其诡异,但是当我真正用的时候,才发现太诡异了。看下面两句:
if (@animals < 5) { ... }#这一句可以判断@animals中元素的个数。
print @animals;#这一句会打印出@animals中的所有元素。
还有下面这个,和ruby很相似的“范围”语法
@animals[0,1]; # gives ("camel", "llama");
@animals[0..2]; # gives ("camel", "llama", "owl");
@animals[1..$#animals]; # gives all except the first element
This is called an “array slice”.
You can do various useful things to lists:
my @sorted = sort @animals;
my @backwards = reverse @numbers;
当然,这里还有很多特殊的数组, 比如@ARGV(the command line arguments to your script) 还有@_(the arguments passed to a subroutine). 后面会有单独的文章介绍。
Hashes
不多说了,一种键值对的数据结构,附带说两句,其实这个数据结构名叫哈希,其实并不是我们熟悉的hash数据结构,因为正统的hash结构其键值之间应该有一种字面意义上的对应关系,特别是应该有一种hash key 生成体制。Perl中的hash没有这个设计。:
my %fruit_color = ("apple", "red", "banana", "yellow");
You can use whitespace and the => operator to lay them out more nicely:
my %fruit_color = (
apple => "red",
banana => "yellow",
);
#To get at hash elements:
$fruit_color{"apple"}; # gives "red"
#You can get at lists of keys and values with keys() and values().
my @fruits = keys %fruit_colors;
my @colors = values %fruit_colors;
如果要使用hash中的一个元素,则用$符号,如果使用整个hash则使用%符号。例如:
Print $fruit_color{"apple"}; # gives "red"
my @fruits = keys %fruit_colors;
变量作用域:
定义一个变量有两种方式,分别为一下两行:
my $var = "value";
$var = "value";
但是,第二行定义的变量将是一个全局变量,而第一行创建的变量是block级别的变量,这一点和JavaScript的语法非常相似。
如下:
my $x = "foo";
my $some_condition = 1;
if ($some_condition) {
my $y = "bar";
print $x; # prints "foo"
print $y; # prints "bar"
}
print $x; # prints "foo"
print $y; # prints nothing; $y has fallen out of scope
条件语句:
这个与ruby几乎一摸一样:
If与unless
if ( condition ) {
...
} elsif ( other condition ) {
...
} else {
...
}
unless:
unless ( condition ) {
...
}
当然也可以写在一行:
print "Yow!" if $zippy;
print "We have no bananas" unless $bananas;
普通循环:
while ( condition ) {
...
}
until ( condition ) {
...
}
print "LA LA LA\n" while 1; # loops forever
for循环:
for ($i = 0; $i <= $max; $i++) {
...
}
foreach循环:
看例子:
#Hash:
my %fruit_color=("apple"=>"red","banana"=>"yellow");
foreach my $key(keys %fruit_color){
print "The value of $key is $fruit_color{$key}","\n";
}
#Array:
my @fruit_arr=("apple","banana","capp","del");
foreach (@fruit_arr){
print $_,"\n";
}
foreach my $i(@fruit_arr){
print $i,"\n";
}
#还可以写到一行,类似于ruby的语法:
print $_,"\n" foreach (@fruit_arr);
#还可以使用范围:
#比如:
print $list[$_] foreach 0 .. $max;
perl中的变量都是预定义的变量。
比如
my $a="";#如果是字符串类型的变量,””等同于false,其他是true
if($a){
print "true";
}else{
print "false";
}
my $a=0;#如果是数字类型的变量,0等同于false,其他是true
if($a){
print "true";
}else{
print "false";
}
my $b;#如果你定义一个变量,没有赋值,那么当你对它操作的时候会自动赋值,如果需要变量为数字型,就赋值为0,如果需要是字符串型,则赋值为””。
$b++;
print($b);