in reply to How do I call a sub using a variable as its name, objectively
#!/usr/bin/perl use warnings; use strict; { package My; sub new { bless {}, shift } sub show { my ($self, $name) = @_; print "Hello $name!\n" } } my $A = 'My'->new; my $method = shift; $A->$method(@ARGV);
Using it:
$ 1.pl show World Hello World!
Otherwise, you need to do a reference/dereference trick:
$A->${\ $ARGV[0] }(@ARGV[1 .. $#ARGV]);
Update: Fixed arguments for the second example. Thaks etj.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: How do I call a sub using a variable as its name, objectively
by etj (Priest) on Oct 29, 2024 at 14:48 UTC |