Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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 ( [id://11124931]=note: print w/replies, xml ) Need Help??


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

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;
  • Comment on Re^5: How can I call a Perl sub with an object from a sub name stored in a var?
  • Download Code

Replies are listed 'Best First'.
Re^6: How can I call a Perl sub with an object from a sub name stored in a var?
by LanX (Saint) on Dec 10, 2020 at 10:21 UTC
    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;
        > 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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11124931]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (9)
As of 2024-04-18 11:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found