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


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

This is not the correct explanation. Deparse helps:

unexpected OP_METHSTART at /usr/share/perl5/B/Deparse.pm line 1674. unexpected OP_METHSTART at /usr/share/perl5/B/Deparse.pm line 1674. sub BEGIN { require v5.38.0; () } use warnings; use strict; no feature ':all'; use feature ':5.38'; use experimental ('class'); BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x5 +5\x55\x55\x55\x55\x55\x55\x55\x55\x51\x55"} use feature 'class'; { package Gear; ; sub ratio : method { XXX; return $chainring / $cog; } sub gear_inches : method { XXX; return ratio(*$rim + $tire * 2); } } say 'Gear'->new('chainring', 52, 'cog', 11, 'rim', 26, 'tire', 1.5)->g +ear_inches;

(I had to modify the code a bit because I don't have 5.40)

So it takes everything that comes after the method name as the argument, so the * is interpreted as a glob dereference.

Note that B::Deparse also struggles with the new class feature.

Replies are listed 'Best First'.
Re^3: Using perlclass 'methods'
by haj (Vicar) on Jul 18, 2024 at 10:20 UTC

    Thanks for the fix!

    This is also a showcase some quirks of the Perl language:

    • Parentheses are optional after method and subroutine calls. A method needs at least one parameter, and it was not provided with the $self-> syntax, so Perl picks up whatever comes next.
    • The * is a valid (but fortunately rarely used these days) sigil. It is not a multiplication here, but indicates a glob reference.
    • Really quirky: Spaces are allowed between a sigil and its identifier. So the identifier of * $rim is $rim, which is a symbolic reference.

    use v5.40.0 implies "strict refs", so Perl complains about *$rim even before trying to use it as the invocant of the method (as I originally thought).