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



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.

Are your Create{ xxxx }Table and Drop{ xxxx }Table routines so
different for various xxxx's that you have to have completely different
subs for each distinct create, drop and xxx combination?

I'm sure you could simplify to

sub create_table {
my ($xxxx) = shift
# create as necessary
}

or even

sub alter_table {
my ($action, $xxxx) = @_;
# create as necessary if $action eq 'create'
# drop as necessary if $action eq 'drop'
}

-jp

.



Relevant Pages

  • Re: How to dynamically generate function name and call it?
    ... I have a perl script that is used to drop/create/update database tables. ... elsif ($TableName eq "TRCO") ...
    (comp.lang.perl.misc)
  • Finding all the links in a Unix file/directory path
    ... I am working on a Perl script that copies files from a log file. ... I have attached a DirGen.pm that creates the sample directory ... my $work_dir = shift; ... sub generate_test_hier_ { ...
    (comp.lang.perl.misc)
  • Re: how to edit a batch file
    ... del test.log ... I have written a perl script to edit some of the lines. ... Is there a specific reason why you are calling the sub this way? ... based on the code fragments that you showed this should replace the ...
    (comp.lang.perl.misc)
  • Calling functions using variable (I mean symbolic refs)
    ... I have a Perl script with lot of functions. ... sub Class1_Method1 { ... The information contained in this electronic message and any attachments to this message are intended ... you are not the intended recipient, please notify the sender at Wipro or Mailadmin@wipro.com immediately ...
    (perl.beginners)
  • Copy characterdata from XML file to XML file
    ... I'm writing a Perl script that has to copy a block of data (nodes numbers ... and coordinates) from one XML formatted file into another XML file. ... My Perl script is shown below. ... The error occurs in the sub 'ReadCharacterData'. ...
    (comp.lang.perl.misc)