NAME

protected - "private" data fields which are inherited by child classes

SYNOPSIS

    package Foo;
    use public      qw(foo bar );
    use private     qw(_private);
    use protected   qw(_pants spoon);
    
    sub squonk {
        my($self) = shift;
        $self->{_pants} = 'Infinite Trousers';
        $self->{spoon}  = 'What stirs me, stirs everything';
        ...
    }
    
    package Bar;
    # Inherits foo, bar, _pants and spoon
    use base qw(Foo);
    ...
    

DESCRIPTION

Protected member.

Restricted data or functionality. An attribute or method only directly accessible to methods of the same class or of a subclass, but inaccessible from any other scope.

From "Object Oriented Perl" by Damian Conway

The protected module implements something like Protected data members you might find in a language with a more traditional OO implementation such as C++.

Protected data members are similar to private ones with the notable exception in that they are inherited by subclasses. This is useful where you have private information which would be useful for subclasses to know as well.

For example: A class which stores an object in a database might have a protected member "_Changed" to keep track of changes to the object so it does not have to waste time re-writing the entire thing to disk. Subclasses of this obviously need a _Changed field as well, but it would be breaking encapsilation if the author had to remember to "use fields qw(_Changed)" (Assuming, of course, they're using fields and not just a plain hash. In which case forget this whole module.)

The Camel Behind The Curtain

In reality, there is little difference between a "protected" variable and a "public" on in Perl. The only real difference is that the protected module doesn't care what the field is called (ie. if it starts with an underscore or not) whereas fields uses the name to determine if the variable is public or private (ie. inherited or not).

AUTHOR

Michael G Schwern <schwern@pobox.com>

SEE ALSO

public, private, fields, Class::Fields, base