2008/12/30 flw <su2admin@gmail.com>
if () 括号内是 bool 上下文,bool 上下文是 scalar 上下文的一种。
因此两种写法实际上是一样的。
undef 和 delete 的区别可以看 perldoc
2008/12/30 smallfish <smallfish@live.cn>:
>
> 1.结果是一样的,不过意思不一样,@array是判断数组是否真假,scalar则是把数组转成变量,里面的值其实是数组的长度了。
> 2.你可以参考:perldoc -f undef 和perdoc -f delete,他们可以给你答案。
> --
> blog : http://hi.baidu.com/smallfish7788
>
> --------------------------------------------------
> From: "Ray" <chenrano2002@gmail.com>
> Sent: Tuesday, December 30, 2008 4:24 PM
> To: "PerlChina 论坛" <perlchina@googlegroups.com>
> Subject: [PerlChina] Question about if (@array)
>
>>
>> Hi, all
>>
>> Would you explain the output of the following perl codes?
>>
>> question 1:
>> Is the sentence "if (@array) " same as "if ( scalar(@array) )"??
>> question 2:
>> what's the difference of "undef $variable " and "delete $variable"??
>>
>> Thanks,
>> Ray
>>
>>
>>
>> -------------------------------------------
>> code----------------------------------
>> #!/usr/bin/perl
>>
>> use strict;
>> use warnings;
>>
>> my @host = ();
>> my @host1 = ('fdfd', 'fdfdfd');
>> my @host2 = undef;
>>
>> if ( @host ) {
>> print "host ok\n";
>> } else {
>> print "host fail\n";
>> }
>>
>> if ( @host1 ) {
>> print "host1 ok\n";
>> } else {
>> print "host1 fail\n";
>> }
>>
>> if ( @host2 ) {
>> print "host2 ok\n";
>> } else {
>> print "host2 fail\n";
>> }
>> -------------------------------------------
>> code----------------------------------
>>
>> and the output is :
>> host fail
>> host1 ok
>> host2 ok
>>
>>
>> >
>>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
您收到此信息是由于您订阅了 Google 论坛"PerlChina 论坛"论坛。
要在此论坛发帖,请发电子邮件到 perlchina@googlegroups.com
要退订此论坛,请发邮件至 perlchina+unsubscribe@googlegroups.com
更多选项,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问该论坛
-~----------~----~----~----~------~----~------~--~---
没有评论:
发表评论