2014-02-19 37 views
0

使用變量我有以下包:更新,並從包

package My::Content; 

our $new = undef; 
our $content = '<header>content-start'.$new.'content-end</header>'; 

1; 

而且下面的腳本:

use My::Content; 
use warnings; 
use strict; 

$My::Content:new = '++test++'; 

print $My::Content:content; 

然而結果是「內容startcontent端」。正如我從這裏理解的那樣,包變量$content在編譯時被設置,並且$ new值被取爲undef,而不管後面的變量更新。是否有可能在$ content計算之前設置$新值?
因爲我希望看到'content-start ++ test ++ content-end'

回答

2

不,但使content函數,它可以在使用點重新計算。

package My::Content; 

our $new = undef; 
sub content { '<header>content-start'.$new.'content-end</header>' } 

use My::Content; 
use warnings; 
use strict; 

$My::Content:new = '++test++'; 

print My::Content:content; 
+0

這就是它!謝謝。 – swserg