in reply to Re: Using perlclass 'methods'
in thread Using perlclass 'methods'

I don't have this new perl version to experiment. But I would expect that WITHIN a class calling a method name would automatically assume it is $self->ratio() (C++ and Java do that). If this is not the case, then why not dispensing the "usual" message: Undefined subroutine &main::ratio. It seems to me this class feature is half-baked and with old materials. And will lead to confusion in debugging.

Replies are listed 'Best First'.
Re^3: Using perlclass 'methods'
by hippo (Archbishop) on Jul 18, 2024 at 09:26 UTC
    It seems to me this class feature is half-baked

    There is a reason why it is experimental. I understand the rationale given by those who have been advocating it but I only partially agree with it. There doesn't seem to be anybody suggesting that this new feature should be used in production.

    I am more than happy to keep using "classic" OOP and reach for one of the light frameworks available on CPAN on those occasions which warrant something a little more sugary.


    🦛

      oops sorry, i forgot it is experimental. Right.

Re^3: Using perlclass 'methods'
by Arunbear (Prior) on Jul 18, 2024 at 10:31 UTC
    C++ and Java do that, but Python does not. Python was the main inspiration for Perl's OOP, and it also requires methods to be invoked via self, even within the class.
Re^3: Using perlclass 'methods'
by haj (Vicar) on Jul 18, 2024 at 10:47 UTC

    Simple things first: It is not the message Undefined subroutine &main::ratio because we are not in package main but in a class Gear where the method is defined.

    Whether Perl could imply $self->ratio when a method is called without an invocant is an interesting question. I guess it is a real challenge to the interpreter: It needs to figure out that ratio is a method and not a sub. As far as I know, this is not possible today. Whether this insurmountable, I can't say. Perhaps we should open an issue for Perl?

    And... of course the class feature is half baked. It is rather new and will take time to mature. For this issue, it behaves like all other OO systems in Perl. It could do different because it has the class and method contexts which the other systems don't have. Perhaps it will, at some point in the future?

      Simple things first: It is not the message Undefined subroutine &main: +:ratio because we are not in package main but in a class Gear where t +he method is defined.

      I assumed that if it does not find ratio() as a method in class then it goes to main looks there. Given that a method always requires its object (e.g. $self->ratio()) then I thought the reasonable thing would be to assume that this is a sub in main. Fair enough. (also I missed the experimental bit, so we shall see). Edit: Oh but it does not know that the object is not preceeding the method, it asusmes that the object is there, that's why it looks further ahead in the multiplicative factors.