在什么情况下,'use fields'invate中的实例变量被声明为'_var'?

时间:2010-06-08 21:25:06

标签: perl oop field private-members

我试图了解fields pragma的行为,我发现poorly documented,关于以下划线为前缀的字段。这就是文档中有关它的说法:

  

以下划线字符开头的字段名称对该类是私有的,并且对子类不可见。可以覆盖继承的字段,但如果与-w开关一起使用,则会生成警告。

根据我的测试,这与其实际行为不一致。不仅_ - 在子类中可见前缀字段,它们在外部类中也是可见的(除非我没有得到“可见”的含义)。此外,直接访问受限制的哈希工作正常。

在哪里可以找到有关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');
}

1 个答案:

答案 0 :(得分:7)

巧合的是,我碰巧在两年前的~/codescraps/fields/test.pl中有一个测试脚本,当时我尝试回答完全相同的问题。 :)

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

{
   package Foo;
   use fields qw(foo bar _Foo_private);
   use private qw(_really_private);
   sub new {
       my Foo $self = shift;
       unless (ref $self) {
           $self = fields::new($self);
           $self->{_Foo_private} = "this is Foo's secret";
       }
       $self->{foo} = 10;
       $self->{bar} = 20;
       return $self;
   }
}

my $foo = Foo->new;
$foo->{foo} = 42;

# this will generate an error: field does not exist
#$foo->{zap} = 42;

print "_Foo_private: " . $foo->{_Foo_private} . "\n";
$foo->{_Foo_private} = 1;
print "_Foo_private: " . $foo->{_Foo_private} . "\n";

print "_really_private: " . $foo->{_really_private} . "\n";
$foo->{_really_private} = 1;
print "_really_private: " . $foo->{_really_private} . "\n";

print Dumper($foo);

# subclassing
{
   package Bar;
   use base 'Foo';
   use fields qw(baz _Bar_private);        # these fields not shared with Foo
   sub new {
       my $class = shift;
       my $self = fields::new($class);
       $self->SUPER::new();                # init base fields
       $self->{baz} = 10;                  # init own fields
       $self->{_Bar_private} = "this is Bar's secret";
       return $self;
   }
}

my $bar = Bar->new;
# these work fine
$bar->{foo} = 1;
$bar->{bar} = 1;
$bar->{_Bar_private} = 1;

# this will not work - underscored fields are not visible to children
$bar->{_Foo_private} = 1;

当我运行你的代码时,我收到错误:

No such pseudo-hash field "_b" at test2.pl line 16.

(第16行是sub b的定义。)你在运行它的架构是什么?使用字段pragma的对象不是简单的祝福hashrefs - 它们是有福的arrayrefs,例如当我修改你的构造函数看起来像这样:

sub new {
    my ( $class ) = @_;
    my Foo $self = fields::new($class);
    $self->{a} = 1; $self->{_b} = 2; $self->{__c} = 3;
    print "I look like: ", Data::Dumper::Dumper($self);
    return $self;
}

我明白了:

I look like: $VAR1 = bless( [
                 bless( {
                          'a' => 1
                        }, 'pseudohash' ),
                 1,
                 2,
                 3
               ], 'Bar' );

作为附言,我觉得有必要指出fields编译指示以及随之而来的base编译指示都已弃用,强烈建议不要使用它们。如今,如果您正在构建一个带有访问器的漂亮OO模块,可以使用Class::Accessor或直接转到Moose