Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: OO - best way to have protected methods

by gargle (Chaplain)
on Aug 17, 2005 at 19:36 UTC ( [id://484554]=note: print w/replies, xml ) Need Help??


in reply to OO - best way to have protected methods

Sorry about the confusion about protected and private. I've came up (thanks to the replies) with the following 'system' to use.


package Base; # Base class to experiment with private and protected methods use warnings; use strict; use Carp; sub new { my $type = shift; my $class = ref $type || $type; my $self = { TEXT => undef, SECRET => undef, NOTSOSECRET => undef, }; my $closure = sub { my $field = shift; if (@_) { $self->{$field} = shift; } return $self->{$field}; }; bless ($closure,$class); return $closure; } # a public accessor to set TEXT sub text { &{ $_[0] }("TEXT", @_[1 .. $#_]) } # a private accessor to set SECRET sub private { caller(0) eq __PACKAGE__ || confess "private method"; &{ $_[0] }("SECRET", @_[1 .. $#_]); } # a public accessor to set SECRET by means of the private method sub secret { private($_[0],@_[1 .. $#_]); } # a protected accessor to set NOTSOSECRET sub protected { caller(0)->isa(__PACKAGE__) || confess "protected method\n"; &{ $_[0] }("NOTSOSECRET", @_[1 .. $#_]); } 1;


package Inherit; # Inherit class to experiment with private and protected methods use warnings; use strict; use Base; use vars qw(@ISA); @ISA = qw(Base); sub inheritSecret { $_[0]->private(@_[1 .. $#_]); } sub inheritNotSoSecret { $_[0]->protected(@_[1 .. $#_]); } 1;


The main routine that calls all:
#!/usr/bin/perl use warnings; use strict; use Base; use Inherit; my $base = Base->new(); $base->text("hello"); print $base->text . "\n"; my $inherit = Inherit->new(); $inherit->text("howdy"); print $inherit->text . "\n"; $base->secret("secret"); print $base->secret . "\n"; # this will get us an error saying private method #$inherit->inheritSecret("verySecret"); #print $inherit->inheritSecret . "\n"; # impossible to access protected directly #$inherit->protected("notsosecret"); #print $inherit->protected . "\n"; # access to NOTSOSECRET by means of a protected method $inherit->inheritNotSoSecret("notsosecret"); print $inherit->inheritNotSoSecret . "\n";

Replies are listed 'Best First'.
Re^2: OO - best way to have protected methods
by gargle (Chaplain) on Aug 19, 2005 at 05:31 UTC
    It just occured to me that I now have public, private and protected variables thrown in for free... TEXT, SECRET and NOTSOSECRET are fully accessible by package Base. Other packages (has-a's and is a's) have to use methode calls.

    Classes which inherit from Base can work with TEXT and NOTSOSECRET.Classes which don't inherit can only access TEXT.

    This works for me I guess. The overhead of calling a method to set/get a variable I can live with, and, actually put to good use to check the setters for invalid parameters.

    Thanks, all, for the comments and the links!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://484554]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-25 06:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found