in reply to Re: Re (tilly) 2: How to call Class method inside an object?
in thread How to call Class method inside an object?

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...)

  • Comment on Re (tilly) 4: How to call Class method inside an object?