我試圖瞭解fields
編譯指示的行爲,我發現poorly documented,有關以下劃線開頭的字段。這是文檔要說的:什麼情況下,實例變量在'use fields'中聲明爲'_var'爲private?
Field names that start with an underscore character are made private to the class and are not visible to subclasses. Inherited fields can be overridden but will generate a warning if used together with the -w switch.
根據我的測試,這不符合它的實際行爲。不僅_
- 在子類中可見的前綴字段,它們在外部類中也是可見的(除非我沒有看到'可見'的含義)。此外,直接訪問受限哈希工作正常。
從哪裏可以找到關於fields
編譯指示行爲的更多信息,缺少源代碼?
{
package Foo;
use strict;
use warnings;
use fields qw/a _b __c/;
sub new {
my ($class) = @_;
my Foo $self = fields::new($class);
$self->a = 1; $self->b = 2; $self->c = 3;
return $self;
}
sub a : lvalue { shift->{a} }
sub b : lvalue { shift->{_b} }
sub c : lvalue { shift->{__c} }
}
{
package Bar;
use base 'Foo';
use strict;
use warnings;
use Data::Dumper;
my $o = Bar->new;
print Dumper $o; ##$VAR1 = bless({'_b' => 2, '__c' => 3, 'a' => 1}, 'Foo');
$o->a = 4; $o->b = 5; $o->c = 6;
print Dumper $o; ##$VAR1 = bless({'_b' => 5, '__c' => 6, 'a' => 4}, 'Foo');
$o->{a} = 7; $o->{_b} = 8; $o->{__c} = 9;
print Dumper $o; ##$VAR1 = bless({'_b' => 8, '__c' => 9, 'a' => 7}, 'Foo');
}
感謝您的回覆。我正在運行x86_64,debian和perl 5.10。我還必須指出,據我所知,「字段」根本不被棄用。然而,自5.9以來,它的實現停止使用僞哈希來支持有限的哈希。而且,我也有一份Conways的OO Perl的副本;我當然知道「場地」和「基地」的替代方案。正如我所說的,我只是想了解編譯指示,並且缺少文檔。 – 2010-06-09 01:30:10
@Pedro:是的,他們可以使用相當長的一段時間,但不再真正支持;我實際上在這裏問了一個關於'fields'的問題,我自己,一會兒回來:http://stackoverflow.com/questions/1168644/why-is-the-fields-pragma-incompatible-with-multiple-inheritance-in-perl - - 並得到相同的「使用穆斯代替」回覆。 :) – Ether 2010-06-09 06:10:08