implementing "protected" in OO perl

S

SNeelakantan_C

Is there a way of making a certain variable 'protected' so that only
methods in derived classes may access it? I would prefer a mechanism
that doesn't wrap the variable in a method call since performance would
slow down by an order of magnitude.

-Shanker
 
A

Anno Siegel

Is there a way of making a certain variable 'protected' so that only
methods in derived classes may access it?

As opposed to what?
I would prefer a mechanism
that doesn't wrap the variable in a method call since performance would
slow down by an order of magnitude.

How do you know that? Access to this variable must dominate your program
in the strangest way for this to be true.

If you don't define an accessor (that is, a method) for the variable, it
doesn't play along with OO at all; there will be no difference between
derived classes and other classes with respect to that variable. Class
variables are not inherited in Perl OO.

Anno
 
R

Robert Sedlacek

Am 15 Jan 2005 16:44:57 -0800 schrieb said:
Is there a way of making a certain variable 'protected' so that only
methods in derived classes may access it? I would prefer a mechanism
that doesn't wrap the variable in a method call since performance would
slow down by an order of magnitude.

I'm thinking of handling it this way:

package Example;
use warnings;
use strict;
use Package::with::brings::used::functions;

sub protected_method {
ensure private caller;
# ..
}
__END__

private gets caller as parameter and has it's own caller. By comparing it
is able to find out if both are equal. If they aren't, it drops an error
object, which is used by ensure to drop an error message (and die). Well,
the names are just quick thoughts. As well as a (maybe) useful kind of use
like:

if( has private caller ) {
# ...
}

hth,
p
 
R

Robert Sedlacek

Am Sun, 16 Jan 2005 03:18:26 +0100 schrieb Robert Sedlacek
I'm thinking of handling it this way:

Whoops. I'm sorry, I misread your post in first way. Have you thought about
using tie()?

sry again,
p
 
S

Shawn Corey

Is there a way of making a certain variable 'protected' so that only
methods in derived classes may access it? I would prefer a mechanism
that doesn't wrap the variable in a method call since performance would
slow down by an order of magnitude.

-Shanker
Short answer no and yes. See perldoc -f my

'my' limits the variable's scope to the file (among other things) so if
your object is in a single file, this will work.

--- Shawn
 
X

xhoster

Is there a way of making a certain variable 'protected' so that only
methods in derived classes may access it? I would prefer a mechanism
that doesn't wrap the variable in a method call since performance would
slow down by an order of magnitude.

My prefered method would be to state my preference in the specs/docs that
coders should not diddle with my privates. Then do a code review, and fire
any coders who diddled with my privates. If someone will only follow good
practise when the language itself forces them to do so, I have to consider
that programmer to be armed, hostile, and dangerous.

Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
Martindap

Latest Threads

Top