package protected; use strict; use vars qw($VERSION); $VERSION = 0.04; use Class::Fields::Fuxor; use Class::Fields::Attribs; sub import { #Dump the class. shift; my $package = caller; add_fields($package, PROTECTED, @_); } return <{_pants} = 'Infinite Trousers'; $self->{spoon} = 'What stirs me, stirs everything'; ... } package Bar; # Inherits foo, bar, _pants and spoon use base qw(Foo); ... =head1 DESCRIPTION =over 4 =item I 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 B<"Object Oriented Perl"> by Damian Conway =back The C 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.) =head2 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). =head1 AUTHOR Michael G Schwern =head1 SEE ALSO L, L, L, L, L =cut