只需閱讀perltooc,其中作者解釋同名元對象。我有一些關於它的問題,我沒有通過搜索找到...關於同名元對象的問題
1. 哈希必須被命名爲對象,但如果對象的名稱如果像My :: Good :: Class ,它的同名散列的名稱是什麼? 我想:
package My::Good::Class
our %Class = (some_data => 1);
sub getEpoHash {
my $class = shift;
my $var = ref($class) || $class;
no strict 'refs';
return \%$var;
}
在我寫我們班%的情況下
...; - 它不起作用,但如果我寫%My :: Good :: Class = ...; - 有用。我不明白!在這種情況下,Class是My :: Good包的散列...或者什麼?!
2. 在文章的例子中有如何使用同名的元對象來創建一個monadic類。但是所有的例子都是用嚴格寫的!在使用$ self之前,我是否必須在每個函數中插入沒有嚴格的'refs',還是有其他方法使用strict來重寫?
這裏是例子:
package Cosmos;
%Cosmos =();
# accessor method for "name" attribute
sub name {
my $self = shift;
$self->{name} = shift if @_;
return $self->{name};
}
# read-only accessor method for "birthday" attribute
sub birthday {
my $self = shift;
die "can't reset birthday" if @_; # XXX: croak() is better
return $self->{birthday};
}
# accessor method for "stars" attribute
sub stars {
my $self = shift;
$self->{stars} = shift if @_;
return $self->{stars};
}
# oh my - one of our stars just went out!
sub supernova {
my $self = shift;
my $count = $self->stars();
$self->stars($count - 1) if $count > 0;
}
# constructor/initializer method - fix by reboot
sub bigbang {
my $self = shift;
%$self = (
name => "the world according to tchrist",
birthday => time(),
stars => 0,
);
return $self; # yes, it's probably a class. SURPRISE!
}
# After the class is compiled, but before any use or require
# returns, we start off the universe with a bang.
__PACKAGE__ -> bigbang();
關於同名散列的描述 - 是的,它是嚴格的,但關於monadic類它不是。 http://perldoc.perl.org/perltooc.html#Monadic-Classes 這是奇怪的..那麼我應該如何創建與'複雜'名稱的包的同名散列,如果'簡單'我剛剛把其名稱。 包Some_Class;我們的%Some_Class 包My :: Good :: Class;我們的%My :: Good :: Class; #語法錯誤! 對不起,不明白它是如何工作在:( – Peter 2012-01-07 00:44:46