...

View Full Version : calling sub with a variable name?



bazz
04-18-2009, 03:29 AM
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

bazz
04-18-2009, 03:51 AM
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, 03: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

bazz
04-18-2009, 04:01 AM
Thanks FishMonger.

That's some reading for tomorrow I think. 4am here now.

bazz

FishMonger
04-18-2009, 04: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, 05:43 AM
A very simple example:


my %coms = (
foo => \&foo,
);
my $var = $coms{foo}->('Hello');
print $var;
sub foo {
return($_[0]);
}

bazz
04-19-2009, 01:36 AM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum