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";

In reply to Re: OO - best way to have protected methods by gargle
in thread OO - best way to have protected methods by gargle

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.