On 3/4/07, Jeff Pang <[EMAIL PROTECTED]> wrote:
snip
In fact nothing at all.I'm also using Perl's OO well.But when programming
with Python,I sometime would like to declare a subroutine as private.For this
private method,someone can't access it from out of the class.So I think that
Perl doesn't have this feature.:-)

Yes, as I said earlier Perl does not have that construct in the
language.  There are ways to achieve the same results.  Some, like the
prepended '_' method, are a social convention that only work if people
behave properly.  Others, like Damian Conway's closure method, work to
a large extent without social norms, but impose a runtime penalty or a
complexity penalty.  You can read more about the closure method in
Damian's book "Objected Oriented Perl" or in this PDF:
www.nas.nasa.gov/News/Techreports/2000/PDF/nas-00-008.pdf.  It mostly
covers ways to protect the data (rather than the methods), but if you
treat the methods as data (more closures) then that technique will
work for them as well.  If you are only concerned with private methods
you can use the caller() function to prevent calls from outside of the
object's package (but note that anyone can reenter the package at any
time by saying "package foo;" in their own code):
#!/usr/bin/perl

foo::foo();
print foo::bar();

package foo;

use Carp;

sub foo { #public
       print bar();
}

sub bar { #private
       croak "bar is a private function" if (caller)[0] ne 'foo';
       return "Hello World!\n";
}

In the end the concepts of private, protected, and public are nothing
more than language level implementations of interface definitions.
Well documented libraries (like the DBI) have no need for these
constructs because the methods you are allowed to use are clearly
spelled out.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to