in reply to Re^5: 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?

Thanks for the code! :)

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

The rule is

> 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

Replies are listed 'Best First'.
Re^7: 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 11:20 UTC

    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;
      > Any defined-but-non-coderef value is stringified

      Correction: any "non-coderef value is stringified" , even undef is stringified to an empty string. ( It's just tricky to create a sub for an empty symbol. :)

      DB<199> $obj = bless {},"tmp" DB<200> $tmp::{''} = sub { say "empty" } DB<201> $meth = undef DB<202> $obj->$meth empty DB<203>

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

        I did try undef, but got warnings, so figured it was verboten. I ought to have continued with my experiments.

        Here's something fun. Acme::Ref breaks on recent Perls, but it's a one line patch to fix it. (The _deref function needs to take a long argument instead of an int.)

        use strict; use warnings; use feature 'say'; { package Local::Class; sub new { my ( $class ) = ( shift ); bless {}, $class; } sub AUTOLOAD { my ( $method ) = ( our $AUTOLOAD =~ /([^:]+)$/ ); if ( $method =~ /^(ARRAY|HASH|SCALAR|REF)/i ) { require Acme::Ref; my $real_coderef = $_[0]->can("HANDLE_$1") or die "Cannot handle $1 method"; splice @_, 1, 0, do { local $SIG{__WARN__} = sub {}; Acme: +:Ref::deref($method); }; goto $real_coderef; } return if $method eq 'DESTROY'; die "Could not load $method via AUTOLOAD"; } sub HANDLE_ARRAY { my ( $self, $array, @args ) = ( shift, shift, @_ ); say for @$array; } sub HANDLE_HASH { say "whatever"; } # etc... }; my $object = 'Local::Class'->new(); my $array = [ "Hello", "world" ]; $object->$array; $object->${\ [ "And", "again" ] };