Public/Private Data/Funcion in Perl OO?

D

Davy

Hi all,

I am a C++ programmer and new to Perl.
Is there some equal idea like Public/Private Data/Funcion in Perl OO?
I found the tutorial provided by Perldoc show all the Data is public??

Thanks!
Davy
 
A

anno4000

Davy said:
Hi all,

I am a C++ programmer and new to Perl.
Is there some equal idea like Public/Private Data/Funcion in Perl OO?
I found the tutorial provided by Perldoc show all the Data is public??

Right about methods (and subs in general). There is only the convention
to let the names of private methods begin with an underscore (_private).
That doesn't stop anyone from using them, but at least they'll know they
are doing something wrong.

Data stored in lexical variables are inaccessible from outside their scope
so they are truly private. This is not true for package variables.

Anno
 
A

AC

Right about methods (and subs in general). There is only the convention
to let the names of private methods begin with an underscore (_private).
That doesn't stop anyone from using them, but at least they'll know they
are doing something wrong.

Data stored in lexical variables are inaccessible from outside their scope
so they are truly private. This is not true for package variables.

Anno

You should check out Damian Conway'a "Perl Best Practices". It covers these
issues exactly. You can use "inside out" objects to hide data and you can
use anonymous code refs to achieve private methods. Its been a while since I
read the book, so I'm not sure about how to get protected behavior.

Allan
 
A

anno4000

AC said:
You should check out Damian Conway'a "Perl Best Practices". It covers these
issues exactly. You can use "inside out" objects to hide data

Right. There is no possible access to the data of an inside-out object,
except through the accessors you provide.
and you can
use anonymous code refs to achieve private methods. Its been a while since I
read the book, so I'm not sure about how to get protected behavior.

I don't have the book to hand (don't books have a way of being where you
aren't?), but I don't see how anonymous code refs could ever participate
in inheritance. For inheritance, methods *must* be named and can't be
strictly private.

Anno
 
U

Uri Guttman

D> I am a C++ programmer and new to Perl.

we know you are new to perl already so stop saying it.

and you should really read a good book on perl before you continue your
education via usenet. it will be much more efficient for both you and
us. learn.perl.org has a good free book and the perl docs are very good
too. read a bunch first before you continue posting again and you will
feel much better. :)

uri
 
J

Jimi-Carlo Bukowski-Wills

Hi,

I have a book called "Writing Perl Modules for CPAN" and it's my favourite
book ever! See if you can get a copy. In it is detailed a way to make
something private and enforcing it by using an anonymous sub.

This book will probably introduce you to Perl from the right direction,
since it makes sense for you to find out what Perl does and doesn't do in
comparison to C++ as soon as possible.

Anyway, the solution is you have your new sub return an anonymous sub that
then sets up the internal data using the my keyword... spot the problem
here??? Yes! Suddenly EVERYTHING is private (well, all of the variables
anyway) so that you need accessors/mutators for everything... which are
difficult to roll automatically in this case.

I guess the short answer is: it's possible, but don't bother!

J

Reference:
Sam Tregar, Writing Perl Modules for CPAN, Apress/Springer NY 2002
(Try www.apress.com or www.springer-ny.com)
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top