Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Re: Re: Re: Re: Tie-ing hashes clobbers data

by demerphq (Chancellor)
on Apr 09, 2002 at 17:01 UTC ( [id://157784]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Re: Re: Tie-ing hashes clobbers data
in thread Tie-ing hashes clobbers data

Invoking private methods as methods will lead to Bad Things.

Interesting. I always implement all methods in module as methods. Private or not.

Consider Dave05s example, he has two method _write and _read, now we agree these methods are private as indicated by the underbar.

However where I disagree with you is what this type of privacy means. To me this means that I shouldnt call this method directly (even though I can, method or function) probably because theres no error checking of parameters or some such equivelent reason. However this does not mean that I should not re-implement these methods as necessary in a sub class. In fact these are precisely the two methods that are the most obvious way to alter the behaviour of his class. If I am forbidden from reimplementing these methods I would be forced to reimplement new(),DESTROY() and add new _write() and _read() methods, effectively rewriting half of his class. But if I can change _write() and _read() directly then I dont have to change the published interface at all. new() would simply call the new _read and likewise with DESTROY.

Now I know there are different schools of thought as to how public and private methods should work, but I know from my experience in Perl that the modules that are the easiest to subclass (excluding those which are _intended_ to be subclassed) are those that have _all_ of their subroutines as methods, leaving the least overriding needing to be done.

Peace,

Yves / DeMerphq
---
Writing a good benchmark isnt as easy as it might look.

  • Comment on Re: Re: Re: Re: Re: Tie-ing hashes clobbers data

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Re: Tie-ing hashes clobbers data
by Anonymous Monk on Apr 09, 2002 at 21:37 UTC
    Consider this:
    use strict; package Foo; sub new { my $class = shift; my $self = bless {}, $class; $self->_init; return $self; } sub _init { my $self = shift; $self->{important_flag} = 1; } package Foo::Bar; use base 'Foo'; sub new { my $class = shift; my $self = $class->SUPER::new(@_); $self->_init('har har har'); return $self; } sub _init { my $self = shift; $self->{ext_prop} = $_[0]; } package main; use Data::Dumper; my $foo = Foo->new; my $bar = Foo::Bar->new; print Dumper([$foo, $bar]); __END__ $VAR1 = [ bless( { 'important_flag' => 1 }, 'Foo' ), bless( { 'ext_prop' => 'har har har' }, 'Foo::Bar' ) ];
    The author of Foo::Bar probably didn't know about Foo's &_init, and shouldn't have needed to either. Private methods are private methods. If you want your method to be overridable you shouldn't use a leading underscore and you should document it. Using method invocation on private methods isn't a problem for the module author, it's a problem for those that try to inherit it. If I use a leading underscore on a method in my module I expect that no one else runs it -- that counts for both deriving and derived classes. I also expect no one to override it. If I wanted that I wouldn't use a leading underscore.
      Hmm, yes. There was an interesting conversation on the CB here today about this matter. Ive got to edit it before I post it, but if you check my homenode youll see it (or a link if I post it elsewhere).

      The rub however was the issue of method namespaces and precisely the issue to which you refer, the meaning of the underbar.

      To me (and it seemed quite a few other people) the underbar says to the consumer of the class, "DONT USE ME", but to the developer/extender of the module it says "THERE ARE SPECIAL RULES FOR USING THIS METHOD". Special mention was made of wrappers for recursive subs and other utility type scenarios.

      Now others see it as you appear to, the cut and dried, "PRIVATE, IMPLEMENTATION MIGHT CHANGE". Personally I think that this interpretation of the convention while perhaps suitable in other languages is not so suitable in Perl. It fails to account for a need for the special rules type functions and marking them as distinct.

      I might be weird, but if a sub is going to change interface then it should be documented accordingly. Simply expecting me or any other programmer to interpret "private" in the same way that you do is unreasonable in a language like perl where there is no concept of privacy (er caveats of course :-)

      Anyway, I really wish you would unmask yourself, itd be easier to carry on this conversation...

      I suppose you have your reasons however. :-)

      Yves / DeMerphq
      ---
      Writing a good benchmark isnt as easy as it might look.

        It [Perl] fails to account for a need for the special rules type functions and marking them as distinct.

        From perltoot:
        Perl doesn't impose restrictions on who gets to use which methods. The public-versus-private distinction is by convention, not syntax. /.../ Occasionally you'll see method names beginning or ending with an underscore or two. This marking is a convention indicating that the methods are private to that class alone and sometimes to its closest acquaintances, its immediate subclasses. But this distinction is not enforced by Perl itself. It's up to the programmer to behave.

        It's at least mentioned in the documentation. I guess some people skip perltoot though and dig right into perlobj. Perhaps this should be mentioned in perlobj too?

        You again:
        I might be weird, but if a sub is going to change interface then it should be documented accordingly.

        But underscored methods are usually not documented and thusly isn't in the interface. They're just internal implementation.

        Simply expecting me or any other programmer to interpret "private" in the same way that you do is unreasonable in a language like perl

        I think it's fair to expect that undocumented methods aren't public.

        ...where there is no concept of privacy (er caveats of course :-)

        By using a function call instead of a method call he does enforce some privacy. He disables you from overriding the routine.

        Anyway, I really wish you would unmask yourself, itd be easier to carry on this conversation...

        I doubt you'd recognize me.

        I suppose you have your reasons however. :-)

        Yes, I haven't registered. :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://157784]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-25 05:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found