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;
|
|---|
| 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 | |
by tobyink (Canon) on Dec 10, 2020 at 11:20 UTC | |
by LanX (Saint) on Dec 10, 2020 at 11:39 UTC | |
by tobyink (Canon) on Dec 10, 2020 at 12:05 UTC |