免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1761 | 回复: 8
打印 上一主题 下一主题

请问exec中的lie to program怎么理解啊? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-02-28 10:01 |只看该作者 |倒序浏览
完整内容可以看这个网页:
http://perldoc.perl.org/functions/exec.html

中间有一句:
If you don't really want to execute the first argument, but want to lie to the program you are executing about its own name, you can specify the program you actually want to run as an "indirect object" (without a comma) in front of the LIST.
  1. $shell = '/bin/csh';
  2. exec $shell '-sh';    # pretend it's a login shell
复制代码
我自己试了试上段代码,效果和在shell下敲一个样子,那为什么还说是“不是真的想执行”呢?
我的E文水平有限,这一段E文理解半天也不知道怎么理解,什么叫execute about its own name啊。。。
请教高人指点,谢谢~~

论坛徽章:
0
2 [报告]
发表于 2012-02-28 12:16 |只看该作者
回复 1# 午夜凋零
  1. 去查一下什么是“indirect object”
复制代码

论坛徽章:
0
3 [报告]
发表于 2012-02-28 16:08 |只看该作者
Perl_Er 发表于 2012-02-28 12:16
回复 1# 午夜凋零


谢谢解答。
你说的indirect object是中间不加逗号这种情况吧,像下面这样:
  1. print STDERR "help!!!\n";
复制代码
map和grep的用法中,也有map {...} LIST这样,中间也没逗号,不知道算不算是indirect object。
不过我还是不理解exec那样做是什么意思。。。。

论坛徽章:
0
4 [报告]
发表于 2012-02-28 16:34 |只看该作者
  1. http://hi.baidu.com/zhujian0805/blog/item/78466a38940396edb211c71f.html
复制代码
回复 3# 午夜凋零


   

论坛徽章:
7
戌狗
日期:2013-12-15 20:43:38技术图书徽章
日期:2014-03-05 01:33:12技术图书徽章
日期:2014-03-15 20:31:17未羊
日期:2014-03-25 23:48:20丑牛
日期:2014-04-07 22:37:44巳蛇
日期:2014-04-11 21:58:0915-16赛季CBA联赛之青岛
日期:2016-03-17 20:36:13
5 [报告]
发表于 2012-02-28 21:14 |只看该作者

我的理解那是 lie to the reader 不是 lie to the program

  1. my $program = 'ls';
  2. my $first_argument = '-lah';
  3. system $program $first_argument;     # ls
  4. system $program, $first_argument;    # ls -lah

复制代码

论坛徽章:
0
6 [报告]
发表于 2012-03-01 11:04 |只看该作者
rubyish 发表于 2012-02-28 21:14
我的理解那是 lie to the reader 不是 lie to the program


先谢谢解答~
不加逗号的地方,我也不明白怎么理解,应该是indirect object的理论,这个我没看明白,下去再看看。
加逗号的地方,我认为是把逗号前后两个变量当成一个数组来处理的,我试了试,把两个变量放进一个数组,再system这个数组,结果完全相同。

论坛徽章:
0
7 [报告]
发表于 2012-03-01 11:10 |只看该作者
Perl_Er 发表于 2012-02-28 16:34
回复 3# 午夜凋零


谢谢解答~
你给这个网页上讲的内容给我的帮助很大,谢谢。

另外,还是不明白If you don't really want to execute the first argument, but want to lie to the program you are executing about its own name.这一句的意思,能帮忙解释一下吗?

论坛徽章:
0
8 [报告]
发表于 2012-03-01 12:09 |只看该作者
本帖最后由 Perl_Er 于 2012-03-01 13:20 编辑
  1. exec PROGRAM LIST
  2. perl认为LIST里面的第一个参数是你要执行命令的名字,但实际上是执行的PROGRAM。比如:

  3. my $cmd = 'ls';
  4. my @args = ( "command","surprise" );

  5. exec { $cmd } @args;

  6. # ./test.pl
  7. command: surprise: No such file or directory   --> 这里系统显示你执行了command, 程序实际执行的ls,但是却告诉你是执行的command。 这里被骗了。

  8. 本来应该显示:
  9. ls: surprise: No such file or directory


  10. This always forces interpretation of the LIST as a multivalued list, even if there is only a single scalar in the list.

  11. Using an indirect object with exec or system is also more secure. This usage (which also works fine with system()) forces interpretation of the arguments as a multivalued list, even if the list had just one argument. That way you're safe from the shell expanding wildcards or splitting up words with whitespace in them.
复制代码
回复 7# 午夜凋零


   

论坛徽章:
0
9 [报告]
发表于 2012-03-01 15:28 |只看该作者
Perl_Er 发表于 2012-03-01 12:09
回复 7# 午夜凋零


终于明白了。呵呵。非常感谢~~
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP