- 論壇徽章:
- 0
|
本帖最后由 obsd178 于 2012-09-12 18:02 編輯
- #!/usr/local/bin/perl5.12.4
- use utf8;
- use 5.12.4;
- use strict;
- my ($circumference, $radius, $pi);
- $pi=3.141592654;
- my $xx;
- $xx=defined($radius);
- say "defined return is:$xx before chomp!";
- my $out1;
- $out1=chomp($radius=<STDIN>);
- say "chomp return is: $out1";
- say "radius is:${radius}space";
- my $out2;
- $out2=defined($radius);
- #$out2=defined(0);
- #$out2=defined(undef);
- say "defined return is:$out2 after chomp!";
- $circumference=2*$pi*$radius;
- if($radius == 0){
- say "The circumference of the circle is ->0<-: $circumference";
- }elsif($radius == 1){
- say "Try it again,pls!";
- }else{
- say "The circumference of the circle is: $circumference";
- }
復(fù)制代碼 輸出結(jié)果:
- # perl ex2-2.pl
- -> 敲下 Enter
- chomp return is: 1
- radius is:space -> 之所以加'space'就是害怕里面是空格
- defined return is: 1 -> 怎么會(huì)得到1 ?
- Try it again,pls!
- # perl ex2-2.pl
- 12
- chomp return is: 1
- radius is:12space -> 看是看不出來有東西的,又或者確實(shí)沒東西?
- defined return is: 1
- 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.
- $out1=chomp($radius=<STDIN>) -> chomp 得到 1沒有問題,這個(gè)時(shí)候 $radius 里面到底存了什么?
- -> Enter 敲下之后 \n 被chomp掉 剩下的是什么東東呢? 得到的結(jié)果是 0
- radius is:space -> 之所以加'space'就是害怕里面是空格? 但是這里卻顯示不出來值?
- $out2=defined($radius); -> 如果是 undef 結(jié)果是false 現(xiàn)在的話是true -> 1 ? 因?yàn)?$radius != undef
- defined return is: 1 -> 怎么會(huì)得到1 ? 因?yàn)?$radius != undef
復(fù)制代碼 ============================================================================================================
修改之后:
- if($radius == 0){
- say "The circumference of the circle is ->0<-: $circumference";
- }elsif($radius == 1){
- say "Try it again,pls!";
- }else{
- say "The circumference of the circle is: $circumference";
- }
復(fù)制代碼 輸出結(jié)果:
- # perl ex2-3.pl
- -> 敲下 Enter
- chomp return is: 1
- radius is:space
- defined return is: 1
- The circumference of the circle is ->0<-: 0 ->結(jié)果0 說明 $radius=0
- # perl ex2-3.pl
- 0 -> 輸入 0
- chomp return is: 1
- radius is:0space
- defined return is: 1
- The circumference of the circle is ->0<-: 0 ->結(jié)果0 說明 $radius=0
復(fù)制代碼- my $out2;
- #$out2=defined($radius);
- $out2=defined(0); -> 1
- $out2=defined(undef); -> 沒有東西
- say "defined return is: $out2";
復(fù)制代碼 總結(jié):
0 != undef
undef 可能會(huì)是 0 或者empty string
但是 say "radius is:${radius}space"; -> 始終得不到結(jié)果?
radius is:space -> 之所以加'space'就是害怕里面是空格? 但是這里卻顯示不出來值?
============================================================================================================
- my $xx;
- $xx=defined($radius);
- say "defined return is:$xx before chomp!";
- my $out2;
- $out2=defined($radius);
- say "defined return is:$out2 after chomp!";
- 結(jié)果:
- # perl ex2-3.pl
- defined return is: before chomp! ->干干凈凈 undef
- chomp return is: 1
- radius is:space
- defined return is:1 after chomp! ->已經(jīng)不再是 undef
- The circumference of the circle is ->0<-: 0
- if($radius == 0){
- say "The circumference of the circle is ->0<-: $circumference";
- } ->結(jié)果0 說明 $radius=0
- -> 這是因?yàn)?$radius和數(shù)字比較的時(shí)候?qū)е伦兂闪藬?shù)字
- -> 最后結(jié)果為 0 是因?yàn)?$radius和數(shù)字相乘的時(shí)候?qū)е伦兂闪藬?shù)字
復(fù)制代碼 |
|