in reply to Re: How to call Class method inside an object?
in thread How to call Class method inside an object?

I prefer the Smalltalk usage because it is extremely descriptive of what is happening.

In Smalltalk, classes are objects. As objects they have methods. Those methods are the class methods.

Instances of the class are objects. As objects they have methods. Those methods are instance methods.

Since the class and the instance are different objects, and moreover different kinds of objects, it comes as no surprise that they have different methods.

None of this is applicable in Perl, of course, since Perl packages are not objects. (Simon Cozens wanted to make that possible, but last I heard it got vetoed as potentially breaking too much old code.) But in languages where classes are themselves objects, the Smalltalk terminology is how you should think about things. And people who come from languages like that (for instance merlyn) tend in Perl to like distinguishing between methods that are meant to be called through the name of the class (eg new) and those that are called from an instance (everything else).

Which is why I, along with merlyn, dislike the construct you sometimes see of:

my $pkg = ref($self) || $self;
Its purpose is to blur the distinction between methods of the class and methods of instances of the class, and we see no reason that that distinction is a bad thing to have.

Replies are listed 'Best First'.
Re: Re (tilly) 2: How to call Class method inside an object?
by chromatic (Archbishop) on Apr 27, 2001 at 23:24 UTC
    What distinction is that? Unless you're carrying around instance data pointed to by your blessed referent (which is the problem with CGI.pm), what's the effective difference?
    package MyClass; sub new { my $class = shift; bless(\$class, $class); } sub do_it { print "Doing something!\n"; } package main; my $c = MyClass->new(); my $class = 'MyClass'; $c->do_it(); $class->do_it();
    Now I'm open to the argument that having good access to data in $self is a big help, but method dispatch works either way. (I just re-read perltootc last night.)
      I am glad you are open to that argument, because that is exactly the issue.

      While you can call a method of the class without having an instantiated object, the right function will be called. Ditto if you call a constructor from an instantiated object. That is because Perl is very, very lax about this.

      But if you assume that either call will actually work, then you are breaking the rule that Thou Shall Not Covet thy Object's Internals.

      Now if the module author wants to, they can blur the distinction between class methods (like new) and instance methods (like pretty much anything else) by making the class method work as an instance method. But (depending on the module) they likely cannot make instance methods work as class methods.

      In other words you can halfway blur the distinction, but you cannot really eliminate it. If you cannot eliminate a fundamental distinction, then I think it is important to teach it. And if you are going to teach it, then what value have you added by adding code to your method to make it harder to see the distinction?

      In short in my Perl code there are class methods, meaning methods that are supposed to be invoked through the class' name. There are instance methods, meaning methods that are supposed to be called from an existing instance.

      But rarely, if ever, do the twain meet.

      (Incidentally since I invoked merlyn's name, I feel that I should point to his post on this same topic...)