免费注册 查看新帖 |

Chinaunix

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

关于chomp 和 defined 和 undef [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-09-12 15:08 |只看该作者 |倒序浏览
本帖最后由 obsd178 于 2012-09-12 18:02 编辑

  1. #!/usr/local/bin/perl5.12.4
  2. use utf8;
  3. use 5.12.4;
  4. use strict;

  5. my ($circumference, $radius, $pi);
  6. $pi=3.141592654;
  7. my $xx;
  8. $xx=defined($radius);
  9. say "defined return is:$xx before chomp!";

  10. my $out1;
  11. $out1=chomp($radius=<STDIN>);
  12. say "chomp return is: $out1";

  13. say "radius is:${radius}space";

  14. my $out2;
  15. $out2=defined($radius);
  16. #$out2=defined(0);
  17. #$out2=defined(undef);
  18. say "defined return is:$out2 after chomp!";

  19. $circumference=2*$pi*$radius;
  20. if($radius == 0){
  21.         say "The circumference of the circle is ->0<-: $circumference";
  22. }elsif($radius == 1){
  23.         say "Try it again,pls!";
  24. }else{
  25.         say "The circumference of the circle is: $circumference";
  26. }

复制代码
输出结果:

  1. # perl ex2-2.pl
  2.                      -> 敲下 Enter
  3. chomp return is: 1
  4. radius is:space      -> 之所以加'space'就是害怕里面是空格
  5. defined return is: 1 -> 怎么会得到1 ?
  6. Try it again,pls!
  7. # perl ex2-2.pl
  8. 12
  9. chomp return is: 1
  10. radius is:12space   -> 看是看不出来有东西的,又或者确实没东西?
  11. defined return is: 1
  12. The circumference of the circle is: 75.398223696

复制代码
疑问:
defined:
To tell whether a value is undef and not the empty string, use the
defined function, which returns false for undef, and true for everything else

undef:
If you try to use this “nothing” as a “numeric something,” it acts like
zero. If you try to use it as a “string something,” it acts like the empty string.

  1. $out1=chomp($radius=<STDIN>) -> chomp 得到 1没有问题,这个时候 $radius 里面到底存了什么?
  2.                              -> Enter 敲下之后 \n 被chomp掉 剩下的是什么东东呢? 得到的结果是 0
  3. radius is:space           -> 之所以加'space'就是害怕里面是空格? 但是这里却显示不出来值?
  4. $out2=defined($radius);  -> 如果是 undef 结果是false 现在的话是true -> 1 ? 因为 $radius != undef

  5. defined return is: 1 -> 怎么会得到1 ? 因为 $radius != undef
复制代码
============================================================================================================
修改之后:

  1. if($radius == 0){
  2.         say "The circumference of the circle is ->0<-: $circumference";
  3. }elsif($radius == 1){
  4.         say "Try it again,pls!";
  5. }else{
  6.         say "The circumference of the circle is: $circumference";
  7. }

复制代码
输出结果:

  1. # perl ex2-3.pl
  2.                       -> 敲下 Enter
  3. chomp return is: 1
  4. radius is:space
  5. defined return is: 1
  6. The circumference of the circle is ->0<-: 0  ->结果0 说明 $radius=0
  7. # perl ex2-3.pl
  8. 0                    -> 输入 0
  9. chomp return is: 1
  10. radius is:0space
  11. defined return is: 1
  12. The circumference of the circle is ->0<-: 0  ->结果0 说明 $radius=0
复制代码
  1. my $out2;
  2. #$out2=defined($radius);
  3. $out2=defined(0);               -> 1
  4. $out2=defined(undef);           -> 没有东西
  5. say "defined return is: $out2";
复制代码
总结:
0 != undef
undef 可能会是 0 或者empty string

但是 say "radius is:${radius}space";  -> 始终得不到结果?
radius is:space           -> 之所以加'space'就是害怕里面是空格? 但是这里却显示不出来值?
============================================================================================================

  1. my $xx;
  2. $xx=defined($radius);
  3. say "defined return is:$xx before chomp!";

  4. my $out2;
  5. $out2=defined($radius);
  6. say "defined return is:$out2 after chomp!";

  7. 结果:

  8. # perl ex2-3.pl
  9. defined return is: before chomp!  ->干干净净 undef

  10. chomp return is: 1
  11. radius is:space
  12. defined return is:1 after chomp!  ->已经不再是 undef
  13. The circumference of the circle is ->0<-: 0

  14. if($radius == 0){
  15.         say "The circumference of the circle is ->0<-: $circumference";
  16. }  ->结果0 说明 $radius=0  
  17. -> 这是因为 $radius和数字比较的时候导致变成了数字
  18. -> 最后结果为 0 是因为 $radius和数字相乘的时候导致变成了数字
复制代码

论坛徽章:
4
15-16赛季CBA联赛之北控
日期:2016-12-06 11:11:0115-16赛季CBA联赛之广夏
日期:2016-12-06 15:04:1515-16赛季CBA联赛之四川
日期:2016-12-06 15:59:51黑曼巴
日期:2016-12-09 20:24:05
2 [报告]
发表于 2012-09-12 15:41 |只看该作者
楼主你好我也是新手说说我个人的理解!
$out1=chomp($radius=<STDIN> -> chomp 得到 1没有问题,这个时候 $radius 里面到底存了什么?
                             -> Enter 敲下之后 \n 被chomp掉 剩下的是什么东东呢?

回车输入之后\n 被chomp掉了没有错误,但是这个变量已经被赋值了已经不是null 内存给他分配了一个空间
至于: defined return is: 1 -> 怎么会得到1 ?

这是因为变量已经被赋值,0代表false 1代表true 所以结果得到1
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP