View Full Version : calling sub with a variable name?
Hi,
Not tried this before so I may find that it is not possible. I want to call a sub routine using a variable (passed param). like this
my %live_menus = $menu_requested($business_id,$business_name,$connect);
Is there a way to achieve this similarly to this attempt? Otherwise, I'll need to use conditional comments, I think.
bazz
tried this as suggested by perldoc but strict doesn't allow it.
my %live_menus = &$menu_requested($business_id,$business_name,$connect);
bazz
FishMonger
04-18-2009, 04:51 AM
What you're looking for is called a dispatch table.
http://en.wikipedia.org/wiki/Dispatch_table
http://www.perlmonks.org/?node_id=456530
Thanks FishMonger.
That's some reading for tomorrow I think. 4am here now.
bazz
FishMonger
04-18-2009, 05:06 AM
Here's a little more reading.
When should I use a dispatch table?
http://www.perlmonks.org/?node_id=587072
KevinADC
04-18-2009, 06:43 AM
A very simple example:
my %coms = (
foo => \&foo,
);
my $var = $coms{foo}->('Hello');
print $var;
sub foo {
return($_[0]);
}
Thanks Kevin.
I think I see how it could work - well some of it at least. I'll wait for a day off, to read up on it fully. The 'hello' bit confuses me but... Oh wait.... as I was writing, it dawned on me that that is where to trigger the sub from. Well at least that saves a question :)
bazz
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.