亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 2011 | 回復(fù): 1
打印 上一主題 下一主題

關(guān)于chomp 和 defined 和 undef [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2012-09-12 15:08 |只看該作者 |倒序?yàn)g覽
本帖最后由 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. }

復(fù)制代碼
輸出結(jié)果:

  1. # perl ex2-2.pl
  2.                      -> 敲下 Enter
  3. chomp return is: 1
  4. radius is:space      -> 之所以加'space'就是害怕里面是空格
  5. defined return is: 1 -> 怎么會(huì)得到1 ?
  6. Try it again,pls!
  7. # perl ex2-2.pl
  8. 12
  9. chomp return is: 1
  10. radius is:12space   -> 看是看不出來有東西的,又或者確實(shí)沒東西?
  11. defined return is: 1
  12. The circumference of the circle is: 75.398223696

復(fù)制代碼
疑問:
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沒有問題,這個(gè)時(shí)候 $radius 里面到底存了什么?
  2.                              -> Enter 敲下之后 \n 被chomp掉 剩下的是什么東東呢? 得到的結(jié)果是 0
  3. radius is:space           -> 之所以加'space'就是害怕里面是空格? 但是這里卻顯示不出來值?
  4. $out2=defined($radius);  -> 如果是 undef 結(jié)果是false 現(xiàn)在的話是true -> 1 ? 因?yàn)?$radius != undef

  5. defined return is: 1 -> 怎么會(huì)得到1 ? 因?yàn)?$radius != undef
復(fù)制代碼
============================================================================================================
修改之后:

  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. }

復(fù)制代碼
輸出結(jié)果:

  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  ->結(jié)果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  ->結(jié)果0 說明 $radius=0
復(fù)制代碼
  1. my $out2;
  2. #$out2=defined($radius);
  3. $out2=defined(0);               -> 1
  4. $out2=defined(undef);           -> 沒有東西
  5. say "defined return is: $out2";
復(fù)制代碼
總結(jié):
0 != undef
undef 可能會(huì)是 0 或者empty string

但是 say "radius is:${radius}space";  -> 始終得不到結(jié)果?
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. 結(jié)果:

  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!  ->已經(jīng)不再是 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. }  ->結(jié)果0 說明 $radius=0  
  17. -> 這是因?yàn)?$radius和數(shù)字比較的時(shí)候?qū)е伦兂闪藬?shù)字
  18. -> 最后結(jié)果為 0 是因?yàn)?$radius和數(shù)字相乘的時(shí)候?qū)е伦兂闪藬?shù)字
復(fù)制代碼

論壇徽章:
4
15-16賽季CBA聯(lián)賽之北控
日期:2016-12-06 11:11:0115-16賽季CBA聯(lián)賽之廣夏
日期:2016-12-06 15:04:1515-16賽季CBA聯(lián)賽之四川
日期:2016-12-06 15:59:51黑曼巴
日期:2016-12-09 20:24:05
2 [報(bào)告]
發(fā)表于 2012-09-12 15:41 |只看該作者
樓主你好我也是新手說說我個(gè)人的理解!
$out1=chomp($radius=<STDIN> -> chomp 得到 1沒有問題,這個(gè)時(shí)候 $radius 里面到底存了什么?
                             -> Enter 敲下之后 \n 被chomp掉 剩下的是什么東東呢?

回車輸入之后\n 被chomp掉了沒有錯(cuò)誤,但是這個(gè)變量已經(jīng)被賦值了已經(jīng)不是null 內(nèi)存給他分配了一個(gè)空間
至于: defined return is: 1 -> 怎么會(huì)得到1 ?

這是因?yàn)樽兞恳呀?jīng)被賦值,0代表false 1代表true 所以結(jié)果得到1
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP