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: Re: [ChopperCharles] private and protected methods: Edit Log



budman
User

Apr 3, 2012, 9:00 PM


Views: 2914
Re: [ChopperCharles] private and protected methods

 

Quote
Okay, I've changed my code around, but there's still a problem:

unless ( (caller)[0]->isa( ref($self) ) ) {
$self->__SetLastError("Access to protected method denied.");
return -1;
}

This doesn't work when the caller is a subclass. If I have class B that inherits from class A, $self will be B, but caller will be A.



Since the subclass is inheriting from the parent, all the methods belong to the subclass, except those methods that are overridden. To access the parents versions, you must use SUPER. Even calling SUPER, as the TacoBell new method does, it still reports the caller as TacoBell.

When I ran a test on the script provided, I see that is the case:

# all the taco bell objects
NEW -> Self: TacoBell Caller: TacoBell
t = TacoBell->new();

Self: Caller: fooBar
__foo -> t->iAmfooBar() success.

Self: TacoBell Caller: main
__foo -> t->__foo("Charles") failed.

Self: TacoBell Caller: TacoBell (this is in the base class)
__foo -> t->burrito() success.


# the foobar objects
NEW -> Self: fooBar Caller: main
f = fooBar->new();

Self: fooBar Caller: main
__foo -> f->__foo("d'oh") failed.


Again, this is all in the perldoc object oriented tutorials.


(This post was edited by budman on Apr 3, 2012, 9:03 PM)


Edit Log:
Post edited by budman (User) on Apr 3, 2012, 9:03 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