Private methods in perl5? 
Author Message
 Private methods in perl5?

Hey-

What's the recommended way of implementing method hiding, (along the
lines of c++ private methods) in perl5?

-Rens

--
  o===============================================================o


  | 12 East 49th Street,  35th floor  |   Phone: (212) 339-2823   |
  | New York, New York         10017  |     Fax: (212) 339-2854   |
  o===============================================================o
     -- IMS is unlikely to share any of the above opinions --



Sat, 03 May 1997 03:02:26 GMT  
 Private methods in perl5?

:
:Hey-
:
:What's the recommended way of implementing method hiding, (along the
:lines of c++ private methods) in perl5?

Oh, might as well just use underbars.

    if (_thwap $self) { ........ }

To get at the $self->_thwap method.  Just be a good citizen and
don't do it outside the class.

--tom
--

: 1.  What is the possibility of this being added in the future?
In the near future, the probability is close to zero.  In the distant
future, I'll be dead, and posterity can do whatever they like...  :-) --lwall



Sat, 03 May 1997 23:00:50 GMT  
 Private methods in perl5?

Quote:

>Hey-

>What's the recommended way of implementing method hiding, (along the
>lines of c++ private methods) in perl5?

Document your interfaces.  The same technique used by Smalltalk programmers.

Dean

(thank Him we don't have to run around putting "virtual" on everything...)



Sat, 03 May 1997 18:56:43 GMT  
 Private methods in perl5?

 rens> What's the recommended way of implementing method hiding, (along the
 rens> lines of c++ private methods) in perl5?

Well, you can do as Tom suggests (_ before the name) or you can just
document the interface well.

HOWEVER, perl has, for some time now, had a way to make an interface
private: caller.

You can always check to see if the caller is in your package. If
not... croak. You can even verify that they are calling from the
correct FILE, in case thay did this:

        Your.pm:
                package Your;
                use Carp;
                sub private {
                        ($p,$f) = caller;
                        if ($p ne Your || $f ne 'Your.pm') {
                                croak "Separational Harassment"
                        }
                        # ... private code here ...
                }
        touch:
                #!/usr/bin/perl
                package Your;
                &private($parts);

If you're real paranoid, you might want to use CORE::caller ;-)

                        -AJS



Sun, 04 May 1997 05:35:35 GMT  
 Private methods in perl5?

:
:HOWEVER, perl has, for some time now, had a way to make an interface
:private: caller.
:
:You can always check to see if the caller is in your package. If
:not... croak. You can even verify that they are calling from the
:correct FILE, in case thay did this:

Interesting: I actually did do this recently, but thought it
overly anal, so didn't admit it. :-)

--tom
--

"Lies written in ink can never disguise facts written in {*filter*}.  {*filter*} debts
 must be repaid in kind.  The longer the delay, the greater the interest."
    --Chinese author Lu Xun, 1926



Sun, 04 May 1997 14:20:44 GMT  
 Private methods in perl5?

Quote:
>>>>> Tom Christiansen writes:


# :
# :Hey-
# :
# :What's the recommended way of implementing method hiding, (along the
# :lines of c++ private methods) in perl5?
#
# Oh, might as well just use underbars.
#
#     if (_thwap $self) { ........ }
#
# To get at the $self->_thwap method.  Just be a good citizen and
# don't do it outside the class.

I've used this construct:

----------------------------
package Foo;

BEGIN {
  # private functions and static data
  package Foo::_private;

  $debug = 0;
  sub f { }

  package Foo;

Quote:
}

sub new {
  my $type = shift;

  my $self = {};

  %{$self} = %params;

  if ($Foo::_private::debug) {
    # you get the picture
  }

  bless $self;

Quote:
}

1;

--
David Avraamides
Goldman, Sachs & Co.
(212) 357-1346



Mon, 05 May 1997 05:00:18 GMT  
 Private methods in perl5?

Quote:
>I've used this construct:

>----------------------------
>package Foo;

>BEGIN {
>  # private functions and static data
>  package Foo::_private;

>  $debug = 0;
>  sub f { }

>  package Foo;
>}

>sub new {
>  my $type = shift;

>  my $self = {};

>  %{$self} = %params;

>  if ($Foo::_private::debug) {
>    # you get the picture
>  }

>  bless $self;
>}

>1;

I've used this, too.  Then one day I wanted to subclass and override one of
the "private" functions.  This is a very frustrating problem.  Look in the
BOT (perlbot manpage) under the section heading "THINKING OF CODE REUSE" for
examples showing what you've shown, examples showing how to "flatten" those
packages, and an argument in favor the flattening.  The examples demonstrate
why you may not want to place private methods in a subpackage.

Dean



Mon, 05 May 1997 19:04:51 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Per Object Private Attribs/Methods

2. Can I make methods private/public?

3. Static method bug in Perl5?

4. perl5 package/method invoc. question

5. Perl5 method chaining

6. Perl5: Inheritance (and thanks for class methods)

7. Perl5 : Calling Parent Method

8. Perl5 indirect method calling --- sometimes

9. $var->$method vs $var->$method()

10. need clarification on Package::method() and Package->method()

11. Proxy methods versus class methods

12. confusion about interface methods and internal class methods

 

 
Powered by phpBB® Forum Software