in reply to How can I call a Perl sub with an object from a sub name stored in a var?
use strict; use warnings; package Foo; sub new { my $pkg = shift; my $self = {}; return $self, $pkg; } sub dispatch { my ($self, $mysub, $myargs) = @_; $self->$mysub($myargs); return; } sub mysub1 { my ($self, $args) = @_; foreach my $arg (split /,/, $args) { print qq{$arg\n}; } } package main; my $foo = Foo->new; $foo->dispatch('mysub1', '1,2,3');
Output
prompt$ perl test.pl 1 2 3
|
|---|