In what circumstances are instance variables declared as '_var' in 'use fields' private?

Posted by Pedro Silva on Stack Overflow See other posts from Stack Overflow or by Pedro Silva
Published on 2010-06-08T21:25:06Z Indexed on 2010/06/09 1:32 UTC
Read the original article Hit count: 243

Filed under:
|
|
|

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

© Stack Overflow or respective owner

Related posts about perl

Related posts about oop