在什么情况下实例变量声明为“_var”?在“使用字段”中私人的?

发布于 2024-09-05 14:12:20 字数 1316 浏览 7 评论 0 原文

我试图理解 fields 编译指示的行为,我发现它很差已记录,有关以下划线前缀的字段。这是文档对此的说明:

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

根据我的测试,这与其实际行为不一致,如下。以 _ 为前缀的字段不仅在子类中可见,而且在外部类中也可见(除非我不明白“可见”的含义)。此外,直接访问受限哈希也可以正常工作。

除了源代码之外,我在哪里可以找到有关 fields pragma 行为的更多信息?

{
    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');
}

I'm trying to understand the behavior of the fields pragma, which I find poorly documented, regarding fields prefixed with underscores. This is what the documentation has to say about it:

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.

This is not consistent with its actual behavior, according to my test, below. Not only are _-prefixed fields visible within a subclass, they are visible within foreign classes as well (unless I don't get what 'visible' means). Also, directly accessing the restricted hash works fine.

Where can I find more about the behavior of the fields pragma, short of going at the source code?

{
    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');
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒绊 2024-09-12 14:12:20

巧合的是,我碰巧在 ~/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 的定义。)你在什么架构上运行这个?使用 fields pragma 的对象不是简单的受祝福的哈希引用 - 它们是受祝福的数组引用,例如,当我将构造函数修改为如下所示时:

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 pragma 均已弃用,强烈建议避免使用它们。如今,如果您希望使用访问器构建一个漂亮的面向对象模块,可以使用 Class ::Accessor 或直接转到 Moose

Coincidentally enough, I happen to have a test script in ~/codescraps/fields/test.pl dated from two years ago when I experimented with answering exactly this same question. :)

#!/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;

And when I run your code, I get the error:

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

(line 16 is the definition for sub b.) What architecture are you running this on? Objects using the fields pragma aren't simple blessed hashrefs -- they are blessed arrayrefs, e.g. when I modify your constructor to look like this:

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 see:

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

As a postscript, I feel obliged to point out that the fields pragma, and the base pragma that goes with it, are both deprecated and one is strongly urged to avoid using them. Nowadays, if you're looking to construct a nice OO module with accessors, one would either use Class::Accessor or go directly to Moose.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文