in reply to Re^3: How can I call a Perl sub with an object from a sub name stored in a var?
in thread How can I call a Perl sub with an object from a sub name stored in a var?

Scratch head ... ?

> $method ... can also be an overloaded object though,

Is it really possible to overload the arrow operator -> for method calls from the RHS???

I only see dereferencing listed ${} @{} %{} &{} *{}

with_assign => '+ - * / % ** << >> x .', assign => '+= -= *= /= %= **= <<= >>= x= .=', num_comparison => '< <= > >= == !=', '3way_comparison'=> '<=> cmp', str_comparison => 'lt le gt ge eq ne', binary => '& &= | |= ^ ^= &. &.= |. |.= ^. ^.=', unary => 'neg ! ~ ~.', mutators => '++ --', func => 'atan2 cos sin exp abs log sqrt int', conversion => 'bool "" 0+ qr', iterators => '<>', filetest => '-X', dereferencing => '${} @{} %{} &{} *{}', matching => '~~', special => 'nomethod fallback ='

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^5: How can I call a Perl sub with an object from a sub name stored in a var?
by tobyink (Canon) on Dec 10, 2020 at 08:22 UTC

    As I said, the string overload is used.

    use strict; use warnings; use feature 'say'; BEGIN { package Local::Farm; sub new { my ( $class ) = ( shift ); bless {}, $class; } sub cow_say { say "Moo"; } sub sheep_say { say "Baa"; } sub horse_say { say "Neigh"; } }; BEGIN { package Local::Random::String; use overload q[""] => 'to_string', fallback => 1; sub new { my ( $class, @options ) = ( shift, @_ ); bless \@options, $class; } sub to_string { my ( $self ) = ( shift ); my $ix = rand @$self; $self->[$ix]; } }; my $object = 'Local::Farm'->new(); my $method = 'Local::Random::String'->new( qw/ cow_say sheep_say / ); $object->$method for 1 .. 10;
      Thanks for the code! :)

      OK now I understand what you intended to say, but IMHO you are over-complicating things.

      The rule is

      • If $method is not a coderef, its string-value is used for a method lookup!

      > As I said, the string overload is used.

      No, if you bless a coderef, it'll still ignore the string-value. No matter if it was overloaded or not.

      use strict; use warnings; use feature 'say'; BEGIN { package Local::Farm; sub new { my ( $class ) = ( shift ); bless {}, $class; } sub cow_say { say "Moo"; } sub sheep_say { say "Baa"; } sub horse_say { say "Neigh"; } }; BEGIN { package Local::Random::String; use overload q[""] => 'to_string', fallback => 1; use Data::Dump qw/pp dd/; my @animal_say; sub still_a_function { say "still_a_function(". pp(\@_).")"; } sub new { my ( $class, @options ) = ( shift, @_ ); # @animal_say = @options; # return bless [], $class; return bless \&still_a_function, $class; } sub to_string { my ( $self ) = ( shift ); my $ix = rand @animal_say; $animal_say[$ix]; } } ; my $object = 'Local::Farm'->new(); my $method = 'Local::Random::String'->new( qw/ cow_say sheep_say / ); $object->$method for 1 .. 10;

      -*- mode: compilation; default-directory: "d:/tmp/pm/" -*- Compilation started at Thu Dec 10 11:17:21 C:/Perl_524/bin\perl.exe -w d:/tmp/pm/overload_string.pl still_a_function([bless({}, "Local::Farm")]) still_a_function([bless({}, "Local::Farm")]) still_a_function([bless({}, "Local::Farm")]) still_a_function([bless({}, "Local::Farm")]) still_a_function([bless({}, "Local::Farm")]) still_a_function([bless({}, "Local::Farm")]) still_a_function([bless({}, "Local::Farm")]) still_a_function([bless({}, "Local::Farm")]) still_a_function([bless({}, "Local::Farm")]) still_a_function([bless({}, "Local::Farm")]) Compilation finished at Thu Dec 10 11:17:22

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        It seems you are right. Any defined-but-non-coderef value is stringified if used as a method. You can even use an unblessed arrayref or hashref as a method name.

        use strict; use warnings; use feature 'say'; my $method = []; { package Local::Class; sub new { my ( $class ) = ( shift ); bless {}, $class; } no strict 'refs'; *{"$method"} = sub { say for @$method }; }; my $object = 'Local::Class'->new(); @$method = ( "Hello", "world" ); $object->$method;