Re: How to dynamically generate function name and call it?



On 09/27/2006 08:31 AM, Hemant Shah wrote:
Folks,

How do I dynamically generate function and then execute the function?

I have a perl script that is used to drop/create/update database tables.
The script has drop/create/update functions for each table, and I
have if statements to call appropriate functions:

Example:

if ($TableName eq "TRAC")
{
CreateTRACTable();
}
elsif ($TableName eq "TRCO")
{
CreateTRCOTable();
}

and so on. I have similar if statements for drop and update.
Instead of having bunch of if statements, I can put the table
names in a hash and call:

if ($Action eq "Create")
{
Create${TableName}Table():
}
elsif ($Action eq "Drop")
{
Drop${TableName}Table():
}

and so on.


How do I do this?

Thanks.


Use a hash, e.g.:
my %tabledispatch = (
TRAC => \CreateTRACTable,
TRCO => \CreateTRCOTable,
);

....
$tabledispatch{$Action}->();


UNTESTED CODE

--
paduille.4058.mumia.w@xxxxxxxxxxxxx
.