2012-01-06 55 views
0

只需閱讀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(); 

回答

2

perltooc的當前版本是嚴格的標準,你可能看的是舊版本。

http://perldoc.perl.org/perltooc.html#The-Eponymous-Meta-Object

最佳實踐隨時間而變化,和許多老的代碼示例將需要大約散落幾no strict 'refs',讓他們與狹窄的工作。

關於包裝用同名的哈希值,如果你有一個名爲My::Good::Class包,你把這個字符串作爲哈希(嚴格的參關閉),你指的是%Class散在My::Good包。

+0

關於同名散列的描述 - 是的,它是嚴格的,但關於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

1

我一樣困惑彼得所以我寫了這一點:

use strict; 
use warnings; 

sub describe 
{ 
    my ($class) = @_; 

    # Ensure class variable hash name contains "::" 
    my $cv = $class . ($class !~ /::/ && "::${class}"); 
    # Convert symbolic ref to "hard" ref 
    no strict "refs"; 
    $cv = \%$cv; 
    use strict; 
    print "$class ($cv): \"", $cv->{description}, "\"\n\n"; 
} 

package Simple; 

# "our" creates a variable in the current package 
# so "our %Simple" is the same as "%Simple::Simple". 

# our %Simple = (description => "Simple's package is " . __PACKAGE__); 
# print "\%Simple at ", \ %Simple, "\n"; 

%Simple::Simple = (description => "Simple's package is " . __PACKAGE__); 
print "\%Simple::Simple at ", \ %Simple::Simple, "\n"; 

main::describe __PACKAGE__; 

package More::Complex; 

%More::Complex = 
    (description => "More::Complex's package is " . __PACKAGE__); 

print "\%More::Complex at ", \ %More::Complex, "\n"; 

main::describe __PACKAGE__; 

什麼這表明的是,在包裝Simple時,他說our %Simple = ...實際上意味着同%Simple::Simple = ...

perltooc.html中的示例將其隱藏,因爲它們從未嘗試在其包的外部引用%Simple。你爲什麼要做這樣的事情?因爲在現實世界中,您不會在每個包中重複使用類變量訪問代碼,而是從某個超類繼承它。超類代碼需要知道類Simple的類變量是%Simple::Simple,如我的describe(sort-of-)方法。

這個小隱疣讓我不知道,如果它不是簡單的對類變量固定的名稱,如%Simple::class_varibles%More::Complex::class_variables,即使你沒有得到通過使用這個詞看起來那麼巧「齊名的」 :-)。