2012-04-25 63 views
11

我的previous question已經解決了我的問題,但讓我缺乏理解。在Perl中關鍵字`no`實際上做了什麼?

use 5.014; 
use warnings; 
use Test::More; 

# still has carp after no Carp 
package Test0 { 
    use Carp qw(carp); 
    sub new { 
     my $class = shift; 
     my $self = {}; 

     carp 'good'; 

     bless $self, $class; 
     return $self; 
    } 
    no Carp; 
} 

my $t0 = Test0->new; 

ok(! $t0->can('carp'), 'cannot carp'); 

這個測試沒有通過,這意味着no ...沒有做什麼,我想這樣做,其中包括unimporting的符號。我讀過perldoc no,但它確實看起來很沒有說服力。鑑於此代碼的結果,我會說它並不完全按照它的廣告。

no是做什麼用的?何時以及爲什麼我應該使用它?

回答

15

no調用一個包的unimport(),而use調用import(),這兩個都默默跳過沒有找到所需子的情況。

然而,很少的軟件包 - 實際上,大多數只有編譯指示模塊 - 支持unimport()

+5

有人可能會爭辯說,'出口商'沒有定義'unimport'來與其「進口」一起是一個錯誤。 – geekosaur 2012-04-25 18:40:58

+0

@geekosaur,我同意。 (可以爭辯:)) – pilcrow 2012-04-25 18:41:46

相關問題