- 论坛徽章:
- 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";
- }
复制代码 输出结果:
- # perl ex2-2.pl
- -> 敲下 Enter
- chomp return is: 1
- radius is:space -> 之所以加'space'就是害怕里面是空格
- defined return is: 1 -> 怎么会得到1 ?
- Try it again,pls!
- # perl ex2-2.pl
- 12
- chomp return is: 1
- radius is:12space -> 看是看不出来有东西的,又或者确实没东西?
- defined return is: 1
- 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.
- $out1=chomp($radius=<STDIN>) -> chomp 得到 1没有问题,这个时候 $radius 里面到底存了什么?
- -> Enter 敲下之后 \n 被chomp掉 剩下的是什么东东呢? 得到的结果是 0
- radius is:space -> 之所以加'space'就是害怕里面是空格? 但是这里却显示不出来值?
- $out2=defined($radius); -> 如果是 undef 结果是false 现在的话是true -> 1 ? 因为 $radius != undef
- defined return is: 1 -> 怎么会得到1 ? 因为 $radius != undef
复制代码 ============================================================================================================
修改之后:
- 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";
- }
复制代码 输出结果:
- # perl ex2-3.pl
- -> 敲下 Enter
- chomp return is: 1
- radius is:space
- defined return is: 1
- The circumference of the circle is ->0<-: 0 ->结果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 ->结果0 说明 $radius=0
复制代码- my $out2;
- #$out2=defined($radius);
- $out2=defined(0); -> 1
- $out2=defined(undef); -> 没有东西
- say "defined return is: $out2";
复制代码 总结:
0 != undef
undef 可能会是 0 或者empty string
但是 say "radius is:${radius}space"; -> 始终得不到结果?
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!";
- 结果:
- # perl ex2-3.pl
- defined return is: before chomp! ->干干净净 undef
- chomp return is: 1
- radius is:space
- defined return is:1 after chomp! ->已经不再是 undef
- The circumference of the circle is ->0<-: 0
- if($radius == 0){
- say "The circumference of the circle is ->0<-: $circumference";
- } ->结果0 说明 $radius=0
- -> 这是因为 $radius和数字比较的时候导致变成了数字
- -> 最后结果为 0 是因为 $radius和数字相乘的时候导致变成了数字
复制代码 |
|