http://www.perlmonks.org?node_id=11160676


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

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?

Replies are listed 'Best First'.
Re^4: Using perlclass 'methods'
by bliako (Abbot) on Jul 18, 2024 at 12:54 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 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.