我发现波浪扩展不适用于以下情况:
$ set -x
$ ./print_arg.pl destdir=~/test
+ ./print_arg.pl 'destdir=/root/test'
destdir=/root/test
$ ./print_arg.pl dest-dir=~/test
+ ./print_arg.pl 'dest-dir=~/test'
dest-dir=~/test
$ ./print_arg.pl -destdir=~/test
+ ./print_arg.pl '-destdir=~/test'
dest-dir=~/test
Run Code Online (Sandbox Code Playgroud)
print_arg.pl的内容是
#!/usr/bin/perl
print $ARGV[0],"\n";
Run Code Online (Sandbox Code Playgroud)
根据Shell的处理顺序,壳牌将在"波浪扩张"之前分词.而且我注意到单词分裂实际上是不同的.结果不同的原因是什么?
我认为以下代码是正常的(并且malloc/free类似):
int foo(){
FILE *fp = fopen("test.in", "r");
int i;
for(i = 0; i < NUM; i ++){
if(Match(fp, i)){
fclose(fp);
return i;
}
}
fclose(fp);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我们可以看到fclose(fp)在代码中出现两次.如果函数foo中有其他return语句,它会显得更多.但是,我必须写fclose(fp)很多次很麻烦.一种解决方案只是一个函数的一个返回.但是,多次返回有时是有用的.还有其他解决方案吗?
PS:据我所知,lisp中有一个宏(带有打开文件).
(with-open-file (stream-var open-argument*)
body-form*)
Run Code Online (Sandbox Code Playgroud)
它可以为我们打开和关闭文件.