home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / protected.pm < prev    next >
Encoding:
Perl POD Document  |  1999-12-14  |  2.4 KB  |  103 lines

  1. package protected;
  2.  
  3. use strict;
  4.  
  5. use vars qw($VERSION);
  6.  
  7. $VERSION = 0.04;
  8.  
  9. use Class::Fields::Fuxor;
  10. use Class::Fields::Attribs;
  11.  
  12. sub import {
  13.     #Dump the class.
  14.     shift;
  15.     
  16.     my $package = caller;
  17.     add_fields($package, PROTECTED, @_);
  18. }
  19.  
  20. return <<TIP;
  21. Protect your member!  Wear a cup!
  22. TIP
  23.  
  24. __END__
  25. =pod
  26.  
  27. =head1 NAME
  28.  
  29. protected - "private" data fields which are inherited by child classes
  30.  
  31.  
  32. =head1 SYNOPSIS
  33.  
  34.     package Foo;
  35.     use public      qw(foo bar );
  36.     use private     qw(_private);
  37.     use protected   qw(_pants spoon);
  38.     
  39.     sub squonk {
  40.         my($self) = shift;
  41.         $self->{_pants} = 'Infinite Trousers';
  42.         $self->{spoon}  = 'What stirs me, stirs everything';
  43.         ...
  44.     }
  45.     
  46.     package Bar;
  47.     # Inherits foo, bar, _pants and spoon
  48.     use base qw(Foo);
  49.     ...
  50.     
  51.  
  52. =head1 DESCRIPTION
  53.  
  54. =over 4
  55.  
  56. =item I<Protected member.>
  57.  
  58. Restricted data or functionality.  An attribute or method only
  59. directly accessible to methods of the same class or of a subclass, but
  60. inaccessible from any other scope.
  61.  
  62. From B<"Object Oriented Perl"> by Damian Conway
  63.  
  64. =back
  65.  
  66. The C<protected> module implements something like Protected data
  67. members you might find in a language with a more traditional OO
  68. implementation such as C++.
  69.  
  70. Protected data members are similar to private ones with the notable
  71. exception in that they are inherited by subclasses.  This is useful
  72. where you have private information which would be useful for
  73. subclasses to know as well.
  74.  
  75. For example: A class which stores an object in a database might have a
  76. protected member "_Changed" to keep track of changes to the object so
  77. it does not have to waste time re-writing the entire thing to disk.
  78. Subclasses of this obviously need a _Changed field as well, but it
  79. would be breaking encapsilation if the author had to remember to "use
  80. fields qw(_Changed)" (Assuming, of course, they're using fields and
  81. not just a plain hash.  In which case forget this whole module.)
  82.  
  83.  
  84. =head2 The Camel Behind The Curtain
  85.  
  86. In reality, there is little difference between a "protected" variable
  87. and a "public" on in Perl.  The only real difference is that the
  88. protected module doesn't care what the field is called (ie. if it
  89. starts with an underscore or not) whereas fields uses the name to
  90. determine if the variable is public or private (ie. inherited or not).
  91.  
  92.  
  93. =head1 AUTHOR
  94.  
  95. Michael G Schwern <schwern@pobox.com>
  96.  
  97.  
  98. =head1 SEE ALSO
  99.  
  100. L<public>, L<private>, L<fields>, L<Class::Fields>, L<base>
  101.  
  102. =cut
  103.