CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: private and protected methods: Edit Log



ChopperCharles
Novice

Mar 28, 2012, 1:02 PM


Views: 10091
private and protected methods

I have an easy way of making methods private:


Code
package foobar; 

my $m_Self = undef;
my $m_LastError = "";

sub new {
my $type = @_;
$m_Self = {};
bless $m_Self, $type;
return $m_Self;
}
sub __foo { #private
if ($_[0] eq $m_Self) {
$m_LastError = "Access to private method denied.";
return -1;
}
my $name = shift(@_);
print "Hello $name.\n";
return 0;
}

sub run {#public, but can also be called from this package.
if ($_[0] eq $self) { shift(@_); }
my $name = shift(@_);
return __foo($name);
}

sub iAmFoobar { #public
return run("foobar");
}
1;


This works great, but now I want to make protected methods. Is there a way I can do this easily? I want to turn __foo into a protected method so that I can use it from a subclass... the only way I've been able to figure out how to call it is like this:


Code
package TacoBell; 
our @ISA = qw(foobar);

my $m_Self = undef;

sub new {
my ($type) = @_;
$m_Self = $type->SUPER::new();
bless $m_Self, $type;
return $m_Self;
}

sub burrito {
return $m_Self->SUPER::__foo("Charles");
}
1;


This doesn't work, for some reason when I call $m_Self->SUPER::__foo("Charles"), $self is added to the parameter list. I've tried this:


Code
 
if ($_[0] eq $self) {
my $me = shift;

if ($me->isa(__PACKAGE__) ){
print("is a package.\n");
} else { print "not a package.\n"; }


but "is a package" always prints, regardless of whether I call $tacobell->burrito() or $tacobell->__foo("Charles");.

How do I go about doing this?

Thanks.

Charles.


(This post was edited by ChopperCharles on Mar 28, 2012, 1:04 PM)


Edit Log:
Post edited by ChopperCharles (Novice) on Mar 28, 2012, 1:03 PM
Post edited by ChopperCharles (Novice) on Mar 28, 2012, 1:04 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives