Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

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


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

...and that's the point. A leading underscore indicates a private method

Er, no.

The point is that its not a method. Its not a private method and its not a public method because it is not a method at all.

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

Replies are listed 'Best First'.
Re: Re: Re: Re: Tie-ing hashes clobbers data
by Anonymous Monk on Apr 09, 2002 at 13:38 UTC
    Method, procedure, function. It's all the same thing. The thing that differes is how you call them. I'd say that
    sub Foo::new { bless {}, $_[0]; }
    is still a method even if I call it with Foo::new($class). Or what about this:
    my $bar = $foo->can('bar'); $bar->($foo); # Is there any difference between this &bar $foo->$bar; # and this &bar?
    There are times when you wish to specify which class's method to use. Here you can call your method as an ordinary subroutine call, being sure to pass the requisite first argument explicitly /.../ Unlike method calls, function calls don't consider inheritance. (perlobj)

    And this is exactly what we want... or at least what Dave05 intended. He even wrote that the subs are private. &_write takes an object as the first argument, so it's effectively the same thing as a method, hence you could say "private method", and private methods shouldn't be invoked as methods. Invoking private methods as methods will lead to Bad Things.
      Method, procedure, function. It's all the same thing. The thing that differes is how you call them. I'd say that
      sub Foo::new { bless {}, $_[0]; }
      is still a method even if you call it with Foo::new($class).

      I can say black is white and white is black all day, but I'm still going to get run over at the next zebra crossing because it's not true. Method calls are invoked using $instance->method_name(). That form searches the @ISA tree. Foo::new() is calling a fully specified subroutine. No amount of wishful thinking will ever make that into a method call. Perl is rather flexible in that it will allow you to call most subroutines either way.

      And strictly speaking (in a computer sciencey manner), there is a specific difference between a procedure and a function. Compare and contrast foldoc's definitions for function and procedure

        I can say black is white and white is black all day

        Or you can ask the author whether it's black or white -- that'll save you from getting run over.

        Method calls are invoked using $instance->method_name().

        Agreed.

        Foo::new() is calling a fully specified subroutine.

        Agreed.

        No amount of wishful thinking will ever make that into a method call.

        When did I claim that? My point was that it's the author that decides whether a subroutine should be a method or not. When I say method I speak of a subroutine that deals with classes/objects and/or is intended to be inherited/overriden, roughly. I believe that's the common interpretation.

        Perl is rather flexible in that it will allow you to call most subroutines either way.

        Exactly. The way we choose to call our subroutine doesn't change what the subroutine is. But if we want to call a private method, we can just as well call it directly -- and we should call it directly. See this node.

        And strictly speaking (in a computer sciencey manner), there is a specific difference between a procedure and a function

        I'm familiar with that from Pascal... But Perl makes no difference. In Perl there's no technical difference between methods, functions, and procedures; they're all subroutines. We are discussing Perl's object model, are we not?
      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.

        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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-03-28 15:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found