in reply to How do I call a sub using a variable as its name, objectively
Choroba showed you how to do it, however allowing calls to arbitrary functions can be a bit of a security hole. To get around that I usually decorate the sub name after the user has provided it:
use strict; use warnings; package My; sub new { my ($class) = @_; return bless {}, $class; } sub ext_show { my ($self, $name) = @_; print "Hello $name!\n"; return 1; } package main; my $A = 'My'->new(); my $method = 'ext_' . shift; $A->$method(@ARGV);
Prints:
Hello Bob!
when given the command line parameters show Bob. Because 'ext_' is added a prefix to the actual name it's not possible for the user to call an unintended sub.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: How do I call a sub using a variable as its name, objectively
by ysth (Canon) on Oct 28, 2024 at 23:21 UTC | |
by GrandFather (Saint) on Oct 29, 2024 at 05:23 UTC |