andreas1234567 has asked for the wisdom of the Perl Monks concerning the following question:
Suppose one a subroutine and a scalar that contains the name of the subroutine. How does one call the subroutine based on the content of the scalar?
use strict; use warnings; sub foo { print 'foo\n'; } sub bar { print 'bar\n'; } my $var = 'foo'; # invoke sub foo __END__
Update Tue Apr 15 07:32:23 CEST 2008: What I am actually trying to do is this:
I intend to create a script that takes two files as arguments: A (CSV) data file, and a (Config::IniFiles) configuration file. The data file will look like this:
The configuration file will look like this:# col1;col2;col3; 123456;hey hey;2008-04-14 123457;my my;2008-04-15
Each key in the configuration file refers to a column in the data file. Each value corresponds to a subroutine that returns true or false depending on whether the data passes validation or not. The list of validation functions grows quickly, and I was hoping to avoid having to maintain an ever-growing list of if-else expressions or a giant hash that maps each string expression to the given subroutine. I am aware of the security risks, although I believe there can be measures put in place to ensure that only pre-defined subroutine can be called.col1=is_int col2=is_string col3=is_yyyy_mm_dd_date
Any tips or hints on how to otherwise solve this problem is welcome.
|
|---|