2015-03-02 72 views
1

你好我試圖引用一個結構的哈希數組,但是當我需要寫它時不起作用。引用散列結構類

use warnings; 
use Class::Struct; 

struct lib => { 
    content => '$', 
    type  => '$', 
}; 

my %library ; 

$library{"one"} = lib->new(); 
$library{"one"}->content("first text here \n"); 
my $ref = \$library{"one"}->content(); 
print $$ref ; 
$$ref = "Second text\n" ; 
print $$ref ; 
print $library{"one"}->content() ; 

如何更新內容的參考?

回答

1

改變結構的內容,只要使用適當的方法:

$library{one}->content("Second text\n"); 

如果你真的想破壞封裝,你可以試試

my $ref = \$library{one}{'lib::content'}; 
$$ref = "Second text\n"; 
print $$ref; 
print $library{one}->content; 
+0

非常感謝你了。它的效果很好 – hrodic 2015-03-02 22:07:30