2016-08-19 121 views
0

我打電話一個Perl子的Perl - 全局符號需要明確包名

&ProcessInput($line, \%txcountershash, \%txhash); 

我有這樣的定義:

sub ProcessInput() {  
my $word; 
my $counterkey; 
my $line = $_[0]; 
my $countershash = $_[1]; 
my $outputhash = $_[2]; 

# remove all the blanks from the line 
$line =~ s/\s+//g; 

my ($port,$counter,$value,$datetime) = split('\|',$line,4); 

my $counterdesc = $countershash{$counter}; 

引起該問題的行是最後一行。它表示全局符號%countershash需要顯式包名稱。我不知道爲什麼它給了我這個錯誤。如果我在腳本運行時註釋該行,則沒有任何其他問題。哈希被設置爲密鑰的錯誤代碼和作爲值的描述。我試圖獲取$ countershash中特定鍵的值,以便將錯誤描述添加到輸出哈希。

+1

此外,不再需要在函數調用的前面加上'&'。 – xxfelixxx

+0

對不起。澄清。它抱怨%反擊。我意識到我已經刪除了錯誤定義$ counter的行 –

+0

將它稱爲'$ countershash - > {$ counter}' – xxfelixxx

回答

0

問題是解引用。您應該取消引用子程序

my $counterdesc = $countershash->{$counter}; 

->內哈希這就是所謂的arrow operator這是用來尊重的數組和散列。

+1

請不要向人們展示可怕的語法。從哈希引用中獲取元素的推薦方法是使用' - >'。所以,'我的$ counterdesc = $ counterhash - > {$ counter};'。 –

+1

@DaveCross感謝您編輯了您的評論文章。 :) – mkHun

0

代碼$counterhash{$counter}表示「在散列%counterhash中查找密鑰$counter」。您沒有名爲%counterhash的散列,您在名爲$counterhash的標量變量中有散列引用。 %counterhash$counterhash是兩個完全不同的變量。

爲了在哈希引用中查找元素,您需要使用不同的語法。

my $counterdesc = $countershash->{$counter}; 

請不要使用$$countershash{$counter}語法(以前)在另一個答案中使用。這隻會混淆誰需要在六個月的時間內維護你的代碼(這可能是你)。

相關問題